Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find IP of computer

Status
Not open for further replies.

rod602

Technical User
May 16, 2011
66
US
Hi Guys,

I found this code online that works for 99.9 % of the computer in my network. But for one system it doesn't get the correct ip, it return 0.0.0.0. Which tells me it is pulling the ip from a network adapter disabled or not active, and since the codes only looks at the first adapter it stops there. For 99.9% of the system on my network this code pulls the correct IP from the correct adapter. I guess I have to make it more robust so that it pulls it pulls the local network LAN adapter Any suggestions on how to do this efficiently? and so that it works on all machines(win xp, vista, win 7).

Thanks
Code:
' Look at IP and act accordingly. 
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
   
strCount = 1

For Each objitem in colitems
    If strCount = 1 Then
        ActiveDHCPIPAddress = objitem.IPAddress(0)
		
        'strIPAddress = Join(objitem.IPAddress, ",")
        IP = stripaddress
        strCount = strCount + 1
        wscript.echo IP
    Else
    End If
next
 
why not just do it for all ip enabled adapters?

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
How does this work? The logic doesn't make much sense. The code also echos IP, which is set to strIPAddress, which is commented out, thus IP will also equal "".

Why not just get all IPs and parse out what you want
Code:
for each objItem in colItems
    strIPs = strIPs & objItem.IPAddress(0) & vbNewLine
next

strFirstIP = left(strIPs, inStr(strIPs, vbNewLine))

or ping the host name to return which IP DNS is using
Code:
set objShell =  CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName
set objExec = objShell.Exec("%comspec% /c ping.exe " & strComputer & "[red] -n 1 -w 100 -4[/red]")

do until objExec.Stdout.AtEndOfStream
	strLine = objExec.StdOut.ReadLine
	if (inStr(strLine, "Reply")) then
		strIP = mid(strLine, 11, inStr(strLine, ":") - 11)
		exit do
	end if
loop

wscript.echo strIP

-n 1 = ping 1 time
-w 100 = wait 100 millisecs between pings
-4 = force IPv4.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
@Geates

Thanks for your response. The only variable I am using is ActiveDHCPIPAddress . The other ones where left in there, after I made modifications.

I guess I could traverse though all IP's from every adapter; and get the one I want. The only IP I care about is the one in the Local LAN.

I am trying to keep the code optimised, and traversing all adapters for all computers doesn't sound efficient for me, if the first adapters has the correct IP.

I guess I can look at the ip, and if its 0.0.0.0, I can look at the next adapter for the host until I find the correct one.

Rigo




 
What makes an adapters IP "correct"? You could also get the a enabled adapters based on the Windows name (like Local Area Connection).

Code:
strIP = "No IP Found"
set objWMI = GetObject("winmgmts:\\.\root\cimv2")
set colNICs = objWMI.ExecQuery("Select * From Win32_NetworkAdapter WHERE NetConnectionID [red]LIKE 'Local Area Connection'[/red]")
	
for each objNIC in colNICs
	set colNICcfg = objWMI.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where MACAddress = '" & objNIC.MACAddress & "' AND IPEnabled = 'true'")
	for each objItem in colNICcfg
		strIP = objItem.IPAddress(0)
	next
next

This code get's all adapters with a name similar to "Local Area Connection" (Typically, there is only one). Then is gets all IPEnabled adapters with that have a MAC address equal to the Local Area Connection.

This approach is slower than then other two methods I posted previously because multiple queries to WMI are executed but it guarantees the data you want if it exists.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I faced this same issue a while back. Was only looking for valid IP addresses from DHCP and not all zero's or APIPA addresses. Here is what I came up with back then.

Code:
'==========================================================================
'
' NAME: GetDHCPIPaddress.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 5/4/2005
'
' COMMENT: Determines current DHCP IP Address of computer
'          Assumes that there is only ONE active NIC.  
'==========================================================================
On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")

Set WSHShell = CreateObject("Wscript.Shell")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
	'WScript.Echo strKeyPath & subkey & "\DhcpIPAddress"
	DHCPAddress = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\DhcpIPAddress")
	If DHCPAddress <> "0.0.0.0" And Left(DHCPAddress,3) <> "169" Then
		ActiveDHCPIPAddress = DHCPAddress	
	End If
Next
WScript.Echo "DHCP Address:" & ActiveDHCPIPAddress

This script will just read the local machine but you can replace the "." in the WMI query to get information from a remote system too as long as remote WMI is not blocked by the Windows Firewall.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top