Get Network Address using VBScript


Like everyone else took a boat load of CISCO Networking classes.  Took night classes at the local community college.

Forgot about it.

But, then last week a Network Engineer asked me what is my network address and I just did not know.

Yes, I know how to issue ipconfig and get my IP Address and Subnet mask.

Or on Linux, issue ifconfig.

But, to think one step ahead and get the Network Address and CIDR, my mind just did not want to go down that step.



Opportunity to code…



REM *********************************************************************************************

'   1a)
'   2a)

'   3a)
REM *********************************************************************************************

option explicit

Dim strLog

Dim strIPAddress
Dim strIPSubnet
Dim strCIDR
Dim strNetworkAddress
Dim strAguments

Dim objAguments
Dim iNumberofArgs 

Const CHAR_PERIOD = "."
function getIPAddress

    REM *****************************************************************************
    REM Rob van der Woude's Scripting Page
    REM Win32_NetworkAdapterConfiguration
    REM *****************************************************************************
    Const strQueryNAC = "select * from Win32_NetworkAdapterConfiguration where MACAddress > ''"
    Const WMISERVICE = "winmgmts://./root/CIMV2"

    Dim objWMISvc
    Dim  objRS
    set objWMISvc = GetObject(WMISERVICE)
    if objWMISvc is Nothing then
        strLog = "Unable to Get Object " & WMISERVICE

        WScript.Echo strLog

        WScript.Quit (-1)
    end if

    Set objRS  = objWMISvc.ExecQuery( strQueryNAC, "WQL", 48 )
    Dim objItem
    For Each objItem In objRS
        'IP Address
        If IsArray( objItem.IPAddress ) Then
            If UBound( objItem.IPAddress ) = 0 Then
                strIPAddress = objItem.IPAddress(0)
                strIPAddress = Join( objItem.IPAddress, "," )
            End If
        End If

        'IP Subnet
        If IsArray( objItem.IPSubnet ) Then
            If UBound( objItem.IPSubnet ) = 0 Then
                strIPSubnet = objItem.IPSubnet(0)
                strIPSubnet = Join( objItem.IPSubnet, "," )
            End If
        End If
end function

'*     bitMask(BitNumber)
'*         Returns a number with all bits set to 0 except for the specified bit
Function bitMask(pBit)

    If pBit < 32 Then 

        bitMask = 2 ^ (pBit - 1) 
        bitMask = "&H80000000"

    End If
End Function

Function Dec2Bin(pValue)

    '*     Dec2Bin(AnyNumber)
    '*         Returns a string representing the number in binary.
    Dim TotalBits, i
    strLog = VarType(pValue)
    Select Case VarType(pValue)
        Case vbLong: 
            TotalBits = 32
        Case vbString: 
            TotalBits = 32
        Case vbInteger: 
            TotalBits = 16
        Case vbByte: 
            TotalBits = 8
        Case Else: 
            strLog = "In Function Dec2Bin:: Value passed is " & pValue & vbCrLf
            strLog = strLog + "VarType(pValue) :- " & CSTR(VarType(pValue)) & vbCrLf

            Wscript.Echo strLog
            Err.Raise 13 ' Not a supported type
    End Select

    For i = TotalBits to 1 Step -1
        If pValue And bitMask(i) Then
            Dec2Bin = Dec2Bin + "1" 
            Dec2Bin = Dec2Bin + "0"
        End if

End Function

Function countSpecficChar( strText, chChar)

    Dim iPos
    Dim iLen
    Dim iCharFound
    Dim iCount
    Dim chCharAtPos
    iPos = 1
    iCount = 0

    iLen = len(strText)
    for iPos = 1 to ILen

        'Get character at position
        chCharAtPos = mid(strText, iPos, 1)

        if (chCharAtPos = chChar) then
            iCount = iCount + 1
        end if
    countSpecficChar = iCount

End Function
function getCIDR(strIPSubnet)

    Dim objArr
    Dim strNumber
    Dim iNumber

    Dim strNumberBin

    Dim iNumberofOnes
    Dim iNumberofOnesTotal
    objArr = Split(strIPSubnet, ".")

    iNumberofOnesTotal = 0
    for each strNumber in objArr
        iNumber = CInt(strNumber)
        strNumberBin = Dec2Bin(iNumber)
        iNumberofOnes = countSpecficChar(strNumberBin, "1")
        iNumberofOnesTotal = iNumberofOnesTotal + iNumberofOnes

    getCIDR = iNumberofOnesTotal
end function

function getNetworkAddress(strIPAddress, strIPSubnet)

    Dim objArrIPAddress
    Dim objArrIPSubnet

    Dim iIPAddress
    Dim iIPSubnet
    Dim strNetworkAddress

    Dim id
    Dim idLowerBound
    Dim idUpperBound
    Dim strLogicalAND
    strNetworkAddress = ""

    'Split Numbers into Array   
    objArrIPAddress = Split(strIPAddress, CHAR_PERIOD)
    objArrIPSubnet = Split(strIPSubnet, CHAR_PERIOD)

    id = 0
    'Get Number of Octets
    idLowerBound = LBOUND(objArrIPAddress)
    idUpperBound = UBOUND(objArrIPAddress) 

    'Transverse Numbers
    for id = idLowerBound to idUpperBound
        iIPAddress = CInt(objArrIPAddress(id))
        iIPSubnet = CInt(objArrIPSubnet(id))
        strLogicalAND = ( iIPAddress AND iIPSubnet )
        'If this is not the first number then add delimeter
        if (strNetworkAddress <> "") Then
            strNetworkAddress = strNetworkAddress + CHAR_PERIOD
        end if
        strNetworkAddress = strNetworkAddress + CSTR(strLogicalAND)

    getNetworkAddress = strNetworkAddress
end function

'Get List of Arguments 
set objAguments = WScript.Arguments

iNumberofArgs = objAguments.Count

if (iNumberofArgs >0) and (iNumberofArgs <> 2)  Then

    set objAguments = Nothing

    Wscript.Echo "Expected two arguments IP Address & Subnet Mask"

elseif (iNumberofArgs =2) Then

    strIPAddress = objAguments(0)
    strIPSubnet = objAguments(1)

    call getIPAddress
end if

set objAguments = Nothing

strNetworkAddress = getNetworkAddress(strIPAddress, strIPSubnet)

strCIDR = getCIDR(strIPSubnet)

WScript.Echo "IP Address :- " & strIPAddress

WScript.Echo "IP Subnet  :- " & strIPSubnet

WScript.Echo "Network Address :- " & CSTR(strNetworkAddress)

WScript.Echo "CIDR :- " & CSTR(strCIDR)


There are two type of invocation.

The first one is to pass along the IP Address and Subnet mask.

And, the other is not pass in any arguments and have the script query the system for its IP Address and subnet mask.


cscript networkAddress.vbs


set _IPAddress=
set _IPSubnet=

cscript networkAddress.vbs %_IPAddress% %_IPSubnet%



Source Control



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s