Subnet mask functions

Since I have no memory (it’s actually this blog’s main purpose!), here is a summary of the possible subnet masks:

Subnet MaskMax HostsMathmaticsHost bit lengthMask octetBinaryMask LengthSubnet Length
255.255.255.25512 ^ 0 =0411111111320
255.255.255.25422 ^ 0 =1411111110311
255.255.255.25242 ^ 0 =2411111100302
255.255.255.24882 ^ 0 =3411111000293
255.255.255.240162 ^ 0 =4411110000284
255.255.255.224322 ^ 0 =5411100000275
255.255.255.192642 ^ 0 =6411000000266
255.255.255.1281282 ^ 0 =7410000000257
255.255.255.02562 ^ 0 =8311111111248
255.255.254.05122 ^ 0 =9311111110239
255.255.252.010242 ^ 0 =103111111002210
255.255.248.020482 ^ 0 =113111110002111
255.255.240.040962 ^ 0 =123111100002012
255.255.224.081922 ^ 0 =133111000001913
255.255.192.0163842 ^ 0 =143110000001814
255.255.128.0327682 ^ 0 =153100000001715
255.255.0.0655362 ^ 0 =162111111111616
255.254.0.01310722 ^ 0 =172111111101517
255.252.0.02621442 ^ 0 =182111111001418
255.248.0.05242882 ^ 0 =192111110001319
255.240.0.010485762 ^ 0 =202111100001220
255.224.0.020971522 ^ 0 =212111000001121
255.192.0.041943042 ^ 0 =222110000001022
255.128.0.083886082 ^ 0 =23210000000923
255.0.0.0167772162 ^ 0 =24111111111824

Since I’m lazy, look in this table is too tiring… pfff… i’m exhausted of typing…
Let’s use a function (thanks to Chris Dent) to generate the mask from its length, and vice versa!

  • Convert a subnet mask to a mask length
  • Function ConvertTo-MaskLength { 
      [CmdLetBinding()]
      Param(
        [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$True)]
        [Net.IPAddress]$SubnetMask
      )
      Process {
        $Bits = "$( $SubnetMask.GetAddressBytes() | ForEach-Object { [Convert]::ToString($_, 2) } )" -Replace '[\s0]'
        Return $Bits.Length
      }
    }
    
  • Convert a mask length to a subnet mask
  • Function ConvertTo-DottedDecimalIP {
    [CmdLetBinding()]
      Param(
        [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
        [String]$IPAddress
      )
       
      Process {
        Switch -RegEx ($IPAddress) {
          "([01]{8}\.){3}[01]{8}" {
            Return [String]::Join('.', $( $IPAddress.Split('.') | ForEach-Object { [Convert]::ToUInt32($_, 2) } ))
          }
          "\d" {
            $IPAddress = [UInt32]$IPAddress
            $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {
              $Remainder = $IPAddress % [Math]::Pow(256, $i)
              ($IPAddress - $Remainder) / [Math]::Pow(256, $i)
              $IPAddress = $Remainder
             } )
            
            Return [String]::Join('.', $DottedIP)
          }
          default {
            Write-Error "Cannot convert this format"
          }
        }
      }
    }
    
    Function ConvertTo-Mask {
      [CmdLetBinding()]
      Param(
        [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$True)]
        [ValidateRange(0, 32)]
        $MaskLength
      )
      Process {
        Return ConvertTo-DottedDecimalIP ([Convert]::ToUInt32($(("1" * $MaskLength).PadRight(32, "0")), 2))
      }
    }
    

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top