PowerShell for Windows Admins

Apr 19 2014   7:52AM GMT

Subnets and prefixes

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: