Using VBScript to determine what subnet a computer is in

Posted on 2008-11-12
Last Modified: 2012-06-22
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 =
strSubnetMak =
strMask 24

What I need to do, is match it to a network address ( 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)

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}!\\" &_


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 <> "" And Left(DHCPAddress,3) <> "169" Then

      strIP = DHCPAddress    

		strMask  = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\DhcpSubnetMask")

		MaskLength strMask

    End If


		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



 MaskLength = intMaskLength

strMaskLength = intMaskLength

'WScript.Echo intMaskLength

End Function

Open in new window

Question by:jlundfelt
    1 Comment
    LVL 41

    Accepted Solution

    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: "
            Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
        End If
        Wscript.Echo "IPEnabled: " & objItem.IPEnabled
        If isNull(objItem.IPSubnet) Then
            Wscript.Echo "IPSubnet: "
            Wscript.Echo "IPSubnet: " & Join(objItem.IPSubnet, ",")
        End If

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This is an explanation of a simple data model to help parse a JSON feed
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now