Jeff Gilbert's Web blog at myITforum.com

This posting is provided "AS IS" with no warranties, and confers no rights :-)
How to determine a client's subnet mask when defining IP subnet boundaries

Lately a lot of people seem to be having some troubles in determining the appropriate subnet mask to use in site boundaries to assign clients when using IP subnet type boundaries (SMS site boundaries and roaming boundaries and Configuration Manager boundaries). When defining boundaries by IP subnets, you can't use superneted addresses--only standard subnet addresses like you would find in DHCP scopes can be used. To get around adding in a ton of IP subnets, you can just ensure that the required IP subnets have been configured for an AD site and use that as the boundary. When using IP subnets as a boundary, the subnets entered must be what clients resolve their subnet mask to be.

To determine the IP subnet that a client believes it lives in (and so what IP subnet to define as a boundary), you have to convert the IP address and subnet mask to binary numbers, keep the bits in the IP address that have bits set in the subnet mask, and then convert the result back to decimal. For example:

A client with a 10.10.67.66 IP address with a subnet mask of 255.255.255.0 would be in the 10.10.67.0 IP subnet...easy enough. Now, change the subnet mask to 255.255.255.192 and what subnet do you have now? <long pause as I get out my binary calculator and do some conversions and give myself a slight headache> Viola! 10.10.67.64. Easy enough right? Well, this is why there is a plethora of subnetting calculators out there I suppose. I just find it easier to run this script on a client computer Smile

Subnetting and I have never really made friends so I love this script. It's an oldie first published in the SMS 2003 Concepts, Planning, and Deployment Guide and I make sure that I always keep this script handy when setting up my lab environments.

For best results, run this from the command prompt using the CSCRIPT option: CSCRIPT subnet.vbs. Get the script HERE (right click and select Save Target As... then change the extension from .txt to .vbs when you're ready to use it).

Here's the script (.vbs): 

'subnet.vbs - displays the subnet for the computer's (or given) IP
' addresses and subnet masks
Set Arguments = Wscript.Arguments
If Wscript.Arguments.Count=2 Then
SubNetIT Arguments(0), Arguments(1)
Wscript.Echo ""
Else
Set loc = CreateObject( "WbemScripting.SWbemLocator" )
Set WbemServices = loc.ConnectServer( ,"root\cimv2" )
Set Adapters=WbemServices.ExecQuery( "Select * FROM" & _
" Win32_NetworkAdapterConfiguration" )
For Each Adapter in Adapters
If NOT IsNull( Adapter.IPAddress) Then
WScript.Echo "Description: ", Adapter.Description
SubNetIt Adapter.IPAddress(0), Adapter.IPSubnet(0)
WScript.Echo ""
End If
Next
WScript.Echo "You can also specify an address and subnet mask as " & _
"parameters to this script."
WScript.Echo ""
End If
WScript.Echo "At least one subnet must be a site's boundary for this computer"
WScript.Echo "to be assigned as a client."
Sub SubNetIt( Address, Subnet )
WScript.Echo "IP address: ", Address
WScript.Echo "subnet mask: ", Subnet
dim addressbytes(4)
dim subnetmaskbytes(4)
i=0
period = 1
while period<>len( address ) + 2
prevperiod=period
period = instr( period+1, address, "." ) + 1
if period = 1 then period = len( address ) + 2
addressbyte = mid( address, prevperiod, period-prevperiod-1 )
addressbytes(i)=addressbyte
i=i+1
wend
i=0
period = 1
while period<>len( subnet ) + 2
prevperiod=period
period = instr( period+1, subnet, "." ) + 1
if period = 1 then period = len( subnet ) + 2
subnetmaskbyte = mid( subnet, prevperiod, period-prevperiod-1)
subnetmaskbytes(i)=subnetmaskbyte
i=i+1
wend
subnet=""
for i=0 to 3
subnet = subnet & (addressbytes(i) AND subnetmaskbytes(i)) & "."
next
subnet = left( subnet, len(subnet)-1 )
WScript.Echo "subnet: ", subnet
End Sub

Hope it helps,

 ~Jeff

Published Tuesday, August 21, 2007 5:39 PM by jgilbert

Comments

# re: How to determine a client's subnet mask when defining IP subnet boundaries@ Thursday, August 23, 2007 6:43 PM

Oh, don't get out the aspirin! And put down the subnet calculator. Here's the easy way to know what subnet they are on: subtract the interesting mask part from 256. In Jeff's example, the only thing that isn't a 255 is 192, so subtract 192 from 256. Anyone? Bueller? 64. That means the first network ID on this mask is 64. The next network ID is the next multiple of 64, or 128. Then 192. Then the next one is 256, so you stop there. If the client's IP address is 10.10.67.145, what network is he on? He's too low to be on the 192 network, so he's on the 128 network.

If your mask is 252, your networks will be multiples of 4, so 10.10.67.66 is on the .64 network.

It even works with good old 255. 256-255 means your networks are multiples of 1.

CaMoya

# karljohn &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;@ Saturday, November 24, 2007 10:57 PM

Pingback from  karljohn &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;

karljohn » How to determine a client's subnet mask when defining IP subnet …

# plut0 &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;@ Sunday, December 09, 2007 11:37 PM

Pingback from  plut0 &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;

plut0 » How to determine a client's subnet mask when defining IP subnet …

# neosimago &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;@ Friday, December 14, 2007 4:34 AM

Pingback from  neosimago &raquo; How to determine a client&#39;s subnet mask when defining IP subnet &#8230;

neosimago » How to determine a client's subnet mask when defining IP subnet …