Subnets and prefixes

Sounds a bit like an old time role playing game but is actually a useful piece of knowledge.

You can define a subnet mask in 2 ways. Either use  the full mask e.g. 255.255.248.0  or define the number of bits in the mask  e.gg 21 which is known as the prefixlength in the PowerShell networking cmdlets.

But can you relate a full subnet mask to the number of bits.  Some are obvious but the others I need to work out. 

Time for a quick PowerShell function:

function show-subnetmask{
  foreach ($prefixlength in  8..30) {
   
    switch ($prefixlength){
   {$_ -gt 24}
               {
                  $bin = (‘1’ * ($prefixlength – 24)).PadRight(8, ‘0’)
                  $o4 = [convert]::ToInt32($bin.Trim(),2)
                 
                  $mask = “255.255.255.$o4”
                  break
                }

    {$_ -eq 24}
                {
                  $mask = ‘255.255.255.0’
                  break
                }

    {$_ -gt 16 -and $_ -lt 24}
               {
                  $bin = (‘1’ * ($prefixlength – 16)).PadRight(8, ‘0’)
                  $o3 = [convert]::ToInt32($bin.Trim(),2)
                 
                  $mask = “255.255.$o3.0”
                  break
                }

    {$_ -eq 16}
                {
                  $mask = ‘255.255.0.0’
                  break
                }
                
    {$_ -gt 8 -and $_ -lt 16}
               {
                  $bin = (‘1’ * ($prefixlength – 8)).PadRight(8, ‘0’)
                  $o2 = [convert]::ToInt32($bin.Trim(),2)
                 
                  $mask = “255.$o2.0.0”
                  break
                }

    {$_ -eq 8} 
                {
                  $mask = ‘255.0.0.0’
                  break
                }
    default    
                {
                  $mask = ‘0.0.0.0’
                }
    }

   
    New-Object -TypeName psobject -Property @{
      PrefixLength = $prefixlength
      Subnetmask = $mask
    }
  }
}

Most people will be using subnets between 8 and 30 bits in length so start with that range and for each value work through the switch statement.  If the value is 8,16 or 24 the subnet mask can be set directly. Otherwise it needs to be calculated. The calculations are the same – the difference is which octet of the subnet mask is affected.

For instance if the prefix length is between 16 and 24 (exclusive)

$bin = (‘1’ * ($prefixlength – 16)).PadRight(8, ‘0’)
$o3 = [convert]::ToInt32($bin.Trim(),2)

$mask = “255.255.$o3.0”
break

Convert the number to a binary representation – the amount you need to subtract depends on the octet with which you are working

Convert the binary to an integer and insert into the subnet mask string.

An object is output that has the subnet mask and prefix length as properties.

Put the function in a module on your module path and you’ll be able to use it as a quick lookup when you need to convert subnet masks to prefix lengths or vice versa

This entry was posted in Networking, Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s