0

I have a powershell code that takes the last subnet created details and thereby finds next available address space with specified /25, /26,/27 or /28 But there are unallocated spaces in between subnets.

Is there a way to find the subnet space between two subnets for a given subnet mask

Need to find unused space and allocate subnets for better management

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • 1
    Yes. give example of your inputs. What format are you looking for output? This is really just counting. Find the next address after the first mask and the get the address just before the second mask. The solution is a range of addresses between the two numbers. – jdweng Nov 23 '22 at 17:39
  • subtract the cidr from 32 (*bits*), then use 2 to the power of the remainder. – Abraham Zinala Nov 23 '22 at 18:31
  • Thess are the subnets already there in my Vnet address 10.0.0.0/16--> 10.0.0.0/24,10.0.1.0/27,10.0.1.32/27,10.0.2.0/27,10.0.3.0/26,10.0.4.0/24. Here I need to find unallocated spaces between them to create a new subnet with any of the /25,/26,/27,/28 ranges – Kolloju_Srinath Nov 24 '22 at 04:31

1 Answers1

0

I tried to write new subnet address space from the list of available subnets

I have followed the below steps to check the subnets which is in the virtual networks

Get-AzureRmVirtualNetwork -Name apgwvnet -ResourceGroupName Alldemorg | Get-AzureRmVirtualNetworkSubnetConfig | Format-Table

enter image description here

I have followed the below script to find out the subnets which are available I have taken the script from the URL

$Virtualnetname = "apgwvnet"
$RGroupName = "Alldemorg"
$Finalsubrange=(Get-AzVirtualNetwork -Name $Virtualnetname -ResourceGroupName $RGroupName).addressspace.addressprefixes
function Get-IpRange {
 [CmdletBinding(ConfirmImpact = 'None')]
    Param(
        [Parameter(Mandatory, HelpMessage = 'Please enter a subnet in the form a.b.c.d/#', ValueFromPipeline, Position = 0)]
        [string[]] $Subnets
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        foreach ($subnet in $subnets) {
            if ($subnet -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$') {
                #Split IP and subnet
                $IP = ($Subnet -split '\/')[0]
                [int] $SubnetBits = ($Subnet -split '\/')[1]
                if ($SubnetBits -lt 7 -or $SubnetBits -gt 30) {
                    Write-Error -Message 'The number following the / must be between 7 and 30'
                    break
                }
                #Convert IP into binary
                #Split IP into different octects and for each one, figure out the binary with leading zeros and add to the total
                $Octets = $IP -split '\.'
                $IPInBinary = @()
                foreach ($Octet in $Octets) {
                    #convert to binary
                    $OctetInBinary = [convert]::ToString($Octet, 2)
                    #get length of binary string add leading zeros to make octet
                    $OctetInBinary = ('0' * (8 - ($OctetInBinary).Length) + $OctetInBinary)
                    $IPInBinary = $IPInBinary + $OctetInBinary
                }
                $IPInBinary = $IPInBinary -join ''
                #Get network ID by subtracting subnet mask
                $HostBits = 32 - $SubnetBits
                $NetworkIDInBinary = $IPInBinary.Substring(0, $SubnetBits)
                #Get host ID and get the first host ID by converting all 1s into 0s
                $HostIDInBinary = $IPInBinary.Substring($SubnetBits, $HostBits)
                $HostIDInBinary = $HostIDInBinary -replace '1', '0'
                #Work out all the host IDs in that subnet by cycling through $i from 1 up to max $HostIDInBinary (i.e. 1s stringed up to $HostBits)
                #Work out max $HostIDInBinary
                $imax = [convert]::ToInt32(('1' * $HostBits), 2) - 1
                $IPs = @()
                #Next ID is first network ID converted to decimal plus $i then converted to binary
                For ($i = 1 ; $i -le $imax ; $i++) {
                    #Convert to decimal and add $i
                    $NextHostIDInDecimal = ([convert]::ToInt32($HostIDInBinary, 2) + $i)
                    #Convert back to binary
                    $NextHostIDInBinary = [convert]::ToString($NextHostIDInDecimal, 2)
                    #Add leading zeros
                    #Number of zeros to add
                    $NoOfZerosToAdd = $HostIDInBinary.Length - $NextHostIDInBinary.Length
                    $NextHostIDInBinary = ('0' * $NoOfZerosToAdd) + $NextHostIDInBinary
                    #Work out next IP
                    #Add networkID to hostID
                    $NextIPInBinary = $NetworkIDInBinary + $NextHostIDInBinary
                    #Split into octets and separate by . then join
                    $IP = @()
                    For ($x = 1 ; $x -le 4 ; $x++) {
                        #Work out start character position
                        $StartCharNumber = ($x - 1) * 8
                        #Get octet in binary
                        $IPOctetInBinary = $NextIPInBinary.Substring($StartCharNumber, 8)
                        #Convert octet into decimal
                        $IPOctetInDecimal = [convert]::ToInt32($IPOctetInBinary, 2)
                        #Add octet to IP
                        $IP += $IPOctetInDecimal
                    }
                    #Separate by .
                    $IP = $IP -join '.'
                    $IPs += $IP
                }
                Write-Output -InputObject $IPs
            } else {
                Write-Error -Message "Subnet [$subnet] is not in a valid format"
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}


$Finalsubrange | Get-IpRange | Out-File iprange.txt

When I run the script I am able to see all the available subnet address

enter image description here

Komali Annem
  • 649
  • 1
  • 1
  • 7