WScript.Echo GetIP ' Sample only, your code here
Function GetIP()
' This function returns the correct IP subnet of the system based on the
' subnet mask. Function first filters out any disconnected NICs or NICs
' that don't have IP protocol enabled. If the NIC doesn't have a Default
' gateway, it is ignored. If there are no default gateways defined, Then
' the function returns 0.0.0.0 as the subnet. If configuration errors are
' found, like multiple active gateways or multiple IP's on one NIC, then the
' function returns 0.0.0.0 as the subnet.
Dim objWMI, objNICInfo, objIPInfo
Dim strActiveAdapter, strIPInfo, strAddress, strGateway, strMask
Dim strSubnet, ctr
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set objNICInfo = objWMI.ExecQuery("Select * from Win32_NetworkAdapter" _
& " Where NetConnectionStatus = 2")
strGateway = Empty
For Each strActiveAdapter In objNICInfo
' Connect to specific adapter by Index number
Set objIPInfo = objWMI.ExecQuery("Select * from " & _
"Win32_NetworkAdapterConfiguration Where Index =" & _
strActiveAdapter.Index)
' If IP is enabled, check for configuration errors, then get IPSubnet
For Each strIPInfo In objIPInfo
If strIPInfo.IPEnabled = True Then
strAddress = Join(strIPInfo.IPAddress, ",")
If IsNull(strIPInfo.DefaultIPGateway) Then
Exit For
Else
strGateway = Join(strIPInfo.DefaultIPGateway, ",")
If InStr(strGateway, ",") Then
Exit For
End If
End If
strMask = Join(strIPInfo.IPSubnet, ",")
If strSubnet = "" Then
strSubnet = GetSubnet(strAddress, strMask)
Else
strSubnet = strSubnet & "," & GetSubnet(strAddress, strMask)
End if
End If
Next
Next
If strGateway = "" Then
MsgBox("No Gateways defined on any NIC")
GetIP = "0.0.0.0"
Exit Function
End If
' Check if multiple subnets were identified
If InStr(strSubnet, ",") Then
MsgBox("Network Misconfiguration! Multiple gateways detected.")
GetIP = "0.0.0.0"
Exit Function
Else
GetIP = strSubnet
End If
End Function
Function GetSubnet(Addr, Mask)
' Get the subnet by "ANDing" the IP address and subnet mask
Dim strNet, Counter
Dim arrIP, arrMask
' Quit function if multiple IP's are bound to one NIC
If InStr(Addr, ",") <> 0 Then
MsgBox("More than one IP bound to NIC.")
GetSubnet = "0.0.0.0"
Exit Function
Else
arrIP = Split(Addr, ".")
End If
arrMask = Split(Mask, ".")
For Counter = 0 To 3
If strNet = "" Then
strNet = arrIP(Counter) And arrMask(Counter)
Else
strNet = strNet & "." & (arrIP(Counter) And arrMask(Counter))
End If
Next
GetSubnet = strNet
End Function