Get IP address of computer, custom, vbscript. Store only valid entry.?

Can someone help me figure out how to script getting a computer ip address. I know there's several vbscripts out there, but our case is a bit different.

Most of our servers have two nics, both enabled, but only one with a valid static ip address, the other 0.0.0.0. How can I retrieve only the valid ip address and store it. And it varies which nic is the valid one, sometimes nic 1 sometimes nic 2

This would be run locally from the computer.
WinPEAsked:
Who is Participating?
 
chandru_solCommented:
Hi,

Try this version.....
I have considered the server subnet as 192.0.0.0. If you want you can this in the line below

  If Left(StrIP,3) = "192" Then

'SCript starts here...
'Declare Variables
Dim j, NIC1, Nic, StrNic, lngCount, StrIP, i ,objNet,objHost

'If something fails, move on
On Error Resume Next

'Get the Computer's network name
Set objNet=CreateObject("wscript.Network")
objHost=objNet.ComputerName
Wscript.Echo "Analysis of " & objHost & "."

'Get a connection to the WMI NetAdapteConfig object
Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

'For Each of the NICs in the connection
For Each Nic in NIC1
   'Get the Adapter Description
   StrNIC = Nic.Description
     'If IP is enabled on the NIC then let's find out about the NIC
     IF Nic.IPEnabled THEN
        lngCount=UBound(Nic.IPAddress)
        For i=0 to lngCount
           If i >= 0 Then
           wscript.echo "===================================================" & _
                vbNewLine
           wscript.echo StrNic & vbNewLine
           StrIP = Nic.IPAddress(i)
           If Left(StrIP,3) = "192" Then
              WScript.Echo vbTab & "IP Address = " & _
                  StrIP

              WScript.Echo vbTab & "MAC Address = " & _
                  Nic.MACAddress

              Wscript.Echo vbTab & "NIC Service (Short) Name = " & _
              Nic.ServiceName

                WScript.Echo vbTab & "IP Subnet(s): "
                For j = 0 to UBound(Nic.IPSubnet)
                    Wscript.Echo vbTab & vbTab & Nic.IPSubnet(j)
                Next
                Wscript.Echo vbTab & "Internet Database Files Path = " & _
                     Nic.DatabasePath
                Wscript.Echo vbTab & "Dead Gateway Detection = " & _
                     Nic.DeadGWDetectEnabled

                Wscript.Echo vbTab & "IP Gateway(s): "
                For j=0 to UBound(Nic.DefaultIPGateway)
                     Wscript.Echo vbTab & vbTab & Nic.DefaultIPGateway(j)
                Next

                If Nic.DHCPEnabled Then
                    Wscript.Echo vbTab & "DHCP Assigned IP address = " & _
                        Nic.DHCPEnabled

                    Wscript.Echo vbTab & "DHCP Server = " & _
                        Nic.DHCPServer
                    End If

                    Wscript.Echo vbTab & "DNS for WINS Resolution Enabled = " & _
                        Nic.DNSEnabledforWINSResolution

                    Wscript.Echo vbTab & "DNS Host Name = " & _
                        Nic.DNSHostName

                    Wscript.Echo vbTab & "DNS Servers:"
                    For j=0 to UBound(Nic.DNSServerSearchOrder)
                        Wscript.Echo vbTab & vbTab & Nic.DNSServerSearchOrder(j)
                    Next

                    Wscript.Echo vbTab & "IP Port Filtering Enabled = " & _
                        Nic.IPFilterSecurityEnabled

                    If Nic.IPFilterSecurityEnabled Then
                        WScript.Echo vbTab & "IP Filtering Enabled."
                         If Nic.IPSecPermitIPProtocols <> 0 Then
                            For j=0 to UBound(Nic.IPSecPermitIPProtocols)
                                 Wscript.Echo vbTab & vbTab & "Protocol: " & _
                                      Nic.IPSecPermitIPProtocols(j)
                            Next
                         Else
                            Wscript.Echo vbTab & vbTab & "No Protocols Filtered"
                         End If

                         If Nic.IPSecPermitTCPPorts <> 0 Then
                            For j=0 to UBound(Nic.IPSecPermitTCPPorts)
                                 WScript.Echo vbTab & vbTab & "TCP Port: " & _
                                       Nic.IPSecPermitTCPPorts(j)
                            Next
                         Else
                            Wscript.Echo vbTab & vbTab & "No TCP Ports Filtered"
                         End If

                         If Nic.IPSecPermitUDPPorts <> 0 Then
                            For j=0 to UBound(Nic.IPSecPermitUDPPorts)
                                 Wscript.Echo vbTab & vbTab & "UDP Port: " & _
                                      Nic.IPSecPermitUDPPorts(j)
                            Next
                          Else
                            WScript.Echo vbTab & vbTab & "No UDP Ports Filtered"
                          End If
                      End If

                      Wscript.Echo vbTab & "LMHOSTS Lookup Enabled = " & _
                             Nic.WINSEnableLMHostsLookup

                      Wscript.Echo vbTab & "WINS Lookup File = " & _
                             Nic.WINSHostLookupFile

                      Wscript.Echo vbTab & "Primary WINS Server = " & _
                             Nic.WINSPrimaryServer

                      Wscript.Echo vbTab & "Secondary WINS Server = " & _
                             Nic.WINSSecondaryServer

                      Wscript.Echo vbTab & "WINS Scope ID = " & Nic.WINSScopeID

                  End If
              End If
            Next
          END IF
      Next

Hope this helps.....
0
 
thenoneCommented:
are the clients dhcp?

If so you can look at dns or dhcp scope.
0
 
WinPEAuthor Commented:
static..
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
WinPEAuthor Commented:
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objAdapter in colAdapters
   If objAdapter.IpAddress(i) <> "" Then
         MyArray = split(objAdapter.IPAddress(i),".")
         strIPAddress = MyArray(0) & "." & MyArray(1) & "." & MyArray(2) & "."
         if debug = 1 Then wscript.echo strIPaddress
         
        

   End If
Next


I can do this to get the ips of both adapters, but again I need a way to get just the valid one.
0
 
thenoneCommented:
Might sound like a dumb question but why is the other nic card configured with 0.0.0.0 if your not using this card why not bridge the nics?
0
 
WinPEAuthor Commented:
The second nic isnt configured to 0.0.0.0, thats just the default when you havent configured a static IP, and there's no dhcp in the network. Secondly we try to disable the NIC that is no in use, but alot of the time this gets left out of the configuration.
0
 
thenoneCommented:
I would bridge the nics and be done with it.
0
 
WinPEAuthor Commented:
We're talking 100's of servers here, not an option. This is besides the point, need to figure out how to script this so it produces the correct ip
0
 
mdiglioCommented:
Hello,
How about doing string manipulation to look fo an IP in your range?
In this example it is looking for the 1st two octets being 123.456

'---added MyArray(3)
strIPAddress = MyArray(0) & "." & MyArray(1) & "." & MyArray(2) & "." & MyArray(3)
'if debug = 1 Then debug.print strIPaddress
If InStr(strIPAddress, "123.456") Then
  wscript.echo strIPAddress
End If

or it could be something like this

strIPAddress = MyArray(0) & "." & MyArray(1) & "." & MyArray(2) & "." & MyArray(3)
'if debug = 1 Then debug.print strIPaddress
If MyArray(0) <> "0"  Then
  wscript.echo strIPAddress
End If

If this works for you let us know and we can deal with the 'storing the info' part of your question
0
 
elissaqpCommented:
This is a script that shows you information per nic, here you can use conditional.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
n = 1
WScript.Echo
 
For Each objAdapter in colAdapters
   WScript.Echo "Network Adapter " & n
   WScript.Echo "================="
   WScript.Echo "  Description: " & objAdapter.Description
 
   WScript.Echo "  Physical (MAC) address: " & objAdapter.MACAddress
   WScript.Echo "  Host name:              " & objAdapter.DNSHostName
 
   If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
         WScript.Echo "  IP address:             " & objAdapter.IPAddress(i)
      Next
   End If
 
   If Not IsNull(objAdapter.IPSubnet) Then
      For i = 0 To UBound(objAdapter.IPSubnet)
         WScript.Echo "  Subnet:                 " & objAdapter.IPSubnet(i)
      Next
   End If
 
   If Not IsNull(objAdapter.DefaultIPGateway) Then
      For i = 0 To UBound(objAdapter.DefaultIPGateway)
         WScript.Echo "  Default gateway:        " & _
             objAdapter.DefaultIPGateway(i)
      Next
   End If
 
   WScript.Echo
   WScript.Echo "  DNS"
   WScript.Echo "  ---"
   WScript.Echo "    DNS servers in search order:"
 
   If Not IsNull(objAdapter.DNSServerSearchOrder) Then
      For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
         WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
      Next
   End If
 
   WScript.Echo "    DNS domain: " & objAdapter.DNSDomain
 
   If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
      For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
         WScript.Echo "    DNS suffix search list: " & _
             objAdapter.DNSDomainSuffixSearchOrder(i)
      Next
   End If
 
   WScript.Echo
   WScript.Echo "  DHCP"
   WScript.Echo "  ----"
   WScript.Echo "    DHCP enabled:        " & objAdapter.DHCPEnabled
   WScript.Echo "    DHCP server:         " & objAdapter.DHCPServer
 
   If Not IsNull(objAdapter.DHCPLeaseObtained) Then
      utcLeaseObtained = objAdapter.DHCPLeaseObtained
      strLeaseObtained = WMIDateStringToDate(utcLeaseObtained)
   Else
      strLeaseObtained = ""
   End If
   WScript.Echo "    DHCP lease obtained: " & strLeaseObtained
 
   If Not IsNull(objAdapter.DHCPLeaseExpires) Then
      utcLeaseExpires = objAdapter.DHCPLeaseExpires
      strLeaseExpires = WMIDateStringToDate(utcLeaseExpires)
   Else
      strLeaseExpires = ""
   End If
   WScript.Echo "    DHCP lease expires:  " & strLeaseExpires
 
   WScript.Echo
   WScript.Echo "  WINS"
   WScript.Echo "  ----"
   WScript.Echo "    Primary WINS server:   " & objAdapter.WINSPrimaryServer
   WScript.Echo "    Secondary WINS server: " & objAdapter.WINSSecondaryServer
   WScript.Echo
 
   n = n + 1
 
Next
 
Function WMIDateStringToDate(utcDate)
   WMIDateStringToDate = CDate(Mid(utcDate, 5, 2)  & "/" & _
                               Mid(utcDate, 7, 2)  & "/" & _
                               Left(utcDate, 4)    & " " & _
                               Mid (utcDate, 9, 2) & ":" & _
                               Mid(utcDate, 11, 2) & ":" & _
                               Mid(utcDate, 13, 2))
End Function
0
 
WinPEAuthor Commented:
Thanks Guy, I'll try this out today
0
 
WinPEAuthor Commented:
Thanks Guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.