Using VBScript to determine what subnet a computer is in

I am tryign to use VBScript to determine what subnet a computer is in. I have the attached snippets of code that return the following;

strIP =192.168.1.100
strSubnetMak = 255.255.255.0
strMask 24

What I need to do, is match it to a network address (192.168.1.0/24) that I am using a separate piece of code to pull from AD

Any thoughts on a easy, and consistent way to do this (there will be around subnets / network addresses to match against, of all different sizes)

TIA !
-Jon
Dim arrOctets
  Dim strBinIP, strBinMask, strIPBit, strMaskBit, strBinNetwork, strMaskLength
  Dim intOctet, i, j
 
On Error Resume Next
 
Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")
 
Set WSHShell = CreateObject("Wscript.Shell")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
For Each subkey In arrSubKeys
 
    DHCPAddress = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\DhcpIPAddress")	
    If DHCPAddress <> "0.0.0.0" And Left(DHCPAddress,3) <> "169" Then
      strIP = DHCPAddress    
		strMask  = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\DhcpSubnetMask")
		MaskLength strMask
    End If
Next
 
		WScript.Echo strIP
		WScript.Echo strMask
		WScript.Echo strMaskLength		
		
Function MaskLength(strMask)
  ' Converts an subnet mask into a mask length in bits
 
  Dim arrOctets
  Dim intOctet, intMaskLength, i, j
 
  arrOctets = Split(strMask, ".")
  For i = 0 To UBound(arrOctets)
    intOctet = CInt(arrOctets(i))
    For j = 0 To 7
      If intOctet And (2^(7 -j)) Then
        intMaskLength = intMaskLength + 1
      End If
    Next
  Next
 MaskLength = intMaskLength
strMaskLength = intMaskLength
'WScript.Echo intMaskLength
End Function

Open in new window

LVL 2
jlundfeltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grayeCommented:
You might consider using Windows Management Instrumentation (WMI) to look into the Win32_NetworkAdapterConfiguration for the IP and Subnet.   Below is a trivial example...
Your function to convert the Subnet into the "class-less" numbering format looks fine

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_NetworkAdapterConfiguration instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Description: " & objItem.Description
    If isNull(objItem.IPAddress) Then
        Wscript.Echo "IPAddress: "
    Else
        Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
    End If
    Wscript.Echo "IPEnabled: " & objItem.IPEnabled
    If isNull(objItem.IPSubnet) Then
        Wscript.Echo "IPSubnet: "
    Else
        Wscript.Echo "IPSubnet: " & Join(objItem.IPSubnet, ",")
    End If
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.