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 Mask | Max Hosts | Mathmatics | Host bit length | Mask octet | Binary | Mask Length | Subnet Length |
---|---|---|---|---|---|---|---|
255.255.255.255 | 1 | 2 ^ 0 = | 0 | 4 | 11111111 | 32 | 0 |
255.255.255.254 | 2 | 2 ^ 0 = | 1 | 4 | 11111110 | 31 | 1 |
255.255.255.252 | 4 | 2 ^ 0 = | 2 | 4 | 11111100 | 30 | 2 |
255.255.255.248 | 8 | 2 ^ 0 = | 3 | 4 | 11111000 | 29 | 3 |
255.255.255.240 | 16 | 2 ^ 0 = | 4 | 4 | 11110000 | 28 | 4 |
255.255.255.224 | 32 | 2 ^ 0 = | 5 | 4 | 11100000 | 27 | 5 |
255.255.255.192 | 64 | 2 ^ 0 = | 6 | 4 | 11000000 | 26 | 6 |
255.255.255.128 | 128 | 2 ^ 0 = | 7 | 4 | 10000000 | 25 | 7 |
255.255.255.0 | 256 | 2 ^ 0 = | 8 | 3 | 11111111 | 24 | 8 |
255.255.254.0 | 512 | 2 ^ 0 = | 9 | 3 | 11111110 | 23 | 9 |
255.255.252.0 | 1024 | 2 ^ 0 = | 10 | 3 | 11111100 | 22 | 10 |
255.255.248.0 | 2048 | 2 ^ 0 = | 11 | 3 | 11111000 | 21 | 11 |
255.255.240.0 | 4096 | 2 ^ 0 = | 12 | 3 | 11110000 | 20 | 12 |
255.255.224.0 | 8192 | 2 ^ 0 = | 13 | 3 | 11100000 | 19 | 13 |
255.255.192.0 | 16384 | 2 ^ 0 = | 14 | 3 | 11000000 | 18 | 14 |
255.255.128.0 | 32768 | 2 ^ 0 = | 15 | 3 | 10000000 | 17 | 15 |
255.255.0.0 | 65536 | 2 ^ 0 = | 16 | 2 | 11111111 | 16 | 16 |
255.254.0.0 | 131072 | 2 ^ 0 = | 17 | 2 | 11111110 | 15 | 17 |
255.252.0.0 | 262144 | 2 ^ 0 = | 18 | 2 | 11111100 | 14 | 18 |
255.248.0.0 | 524288 | 2 ^ 0 = | 19 | 2 | 11111000 | 13 | 19 |
255.240.0.0 | 1048576 | 2 ^ 0 = | 20 | 2 | 11110000 | 12 | 20 |
255.224.0.0 | 2097152 | 2 ^ 0 = | 21 | 2 | 11100000 | 11 | 21 |
255.192.0.0 | 4194304 | 2 ^ 0 = | 22 | 2 | 11000000 | 10 | 22 |
255.128.0.0 | 8388608 | 2 ^ 0 = | 23 | 2 | 10000000 | 9 | 23 |
255.0.0.0 | 16777216 | 2 ^ 0 = | 24 | 1 | 11111111 | 8 | 24 |
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 } }
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)) } }