Get Network Address using VBScript

Background

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.

 

Code

Opportunity to code…

 

VBScript



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

'   1a) http://chris.wastedhalo.com/2014/05/more-binarybitwise-functions-for-vbscript/
    
'   2a) http://www.robvanderwoude.com/vbstech_network_ip.php

'   3a) http://powerasp.net/content/new/vbscript-constants.asp
    
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 http://www.robvanderwoude.com/vbstech_network_ip.php
    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)
        
            Else
            
                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)
        
            Else
            
                strIPSubnet = Join( objItem.IPSubnet, "," )
            
            End If
        
        End If
        
    Next
    
    
    
end function

'*******************************************************************************
'*     bitMask(BitNumber)
'*         Returns a number with all bits set to 0 except for the specified bit
'*     http://chris.wastedhalo.com/2014/05/more-binarybitwise-functions-for-vbscript/
'*******************************************************************************
Function bitMask(pBit)

    If pBit < 32 Then 

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

    End If
    
End Function



Function Dec2Bin(pValue)

    '*************************************************************************************
    '*     Dec2Bin(AnyNumber)
    '*         Returns a string representing the number in binary.
    '*     http://chris.wastedhalo.com/2014/05/more-binarybitwise-functions-for-vbscript/
    '*************************************************************************************
    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
    
            WScript.Quit
            
    End Select

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

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
        
    Next
    
    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
        
    next

    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)
    
    next

    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"
    Wscript.Quit

elseif (iNumberofArgs =2) Then

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

    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)

Invocation

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.

Automatic


cscript networkAddress.vbs

Manual



set _IPAddress=10.0.4.101
set _IPSubnet=255.255.255.128

cscript networkAddress.vbs %_IPAddress% %_IPSubnet%

Output

 

Source Control

GitHub

Link

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s