Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
'==========================================================================
' NAME: GetIPSubnet.vbs
'
' AUTHOR: Paul Chapman, Halcyon Dreams
' DATE : 1-31-2006
'
' COMMENT: Get correct subnet based on subnet mask. WMI call only works On
' Windows XP and higher. Checks for configuration errors such as multiple
' gateways or multiple IPs bound to one NIC.
'
'==========================================================================
Option Explicit
WScript.Echo 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 quits with no value. If configuration errors are found,
' like multiple active gateways or multiple IP's on one NIC, then the
' function quits with no value.
Function GetIP()
Dim objWMI, objNICInfo, objIPInfo
Dim strActiveAdapter, strIPInfo, strAddress, strGateway, strMask, strSubnet
' Connect to WMI
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
' Select adapters that are enabled
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
' No Gateway Defined on NIC, try next Adapter
Exit For
Else
strGateway = Join(strIPInfo.DefaultIPGateway, ",")
If InStr(strGateway, ",") Then
WScript.Echo "Multiple Gateways bound to NIC:" & VbCrLf & strActiveAdapter.Name
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
WScript.Echo "No Gateways defined on any active NIC"
End If
' Check if multiple subnets were identified
If InStr(strSubnet, ",") Then
WScript.Echo "Network Misconfiguration! Multiple Default Gateways"
Exit Function
Else
GetIP = strSubnet
End If
End Function
' Get the subnet by "ANDing" the IP address and subnet mask
Function GetSubnet(Addr, Mask)
Dim strNet, strNum
Dim arrIP, arrMask
' Quit function if multiple IP's are bound to one NIC
If InStr(Addr, ",") <> 0 Then
MsgBox("More than one IP is bound to network adapter: " & VbCrLf & strActiveAdapter.NetConnectionID)
Exit Function
Else
arrIP = Split(Addr, ".")
End If
arrMask = Split(Mask, ".")
For strNum = 0 To 3
If strNet = "" Then
strNet = arrIP(strNum) And arrMask(strNum)
Else
strNet = strNet & "." & (arrIP(strNum) And arrMask(strNum))
End If
Next
GetSubnet = strNet
End Function