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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ping IP address, extract Computer Name 1

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I've been searching how to ping an IP address to extract the computer name.

For instance, if I execute

Code:
ping -n 2 -a [IP Address]

I get the following output

Code:
Pinging HostName.suffix.suffix.suffix [IP Address] with 32 bytes of data:

Reply from IP Address: bytes=32 time=7ms TTL=121
Reply from IP Address: bytes=32 time<1ms TTL=121

So far, I couldn't find a way to save the line

Code:
Pinging HostName.suffix.suffix.suffix [IP Address] with 32 bytes of data

to a string. Otherwise, I could split the string and extract

Code:
HostName.suffix.suffix.suffix


I prefer to get computer name from ping, because there are cases one cannot connect to WMI.
 
Have a look at the WshShell.Exec method and the WshScriptExec.StdOut property.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I saw you problem and thought it would be a good test of my basic scripting skills. Here's a solution I came up with. I'm sure there are more elegant ways to do it, but this is the code I came up with after testing with a known networked IP and non networked IP

Code:
Set objShell = CreateObject("WScript.Shell")
strIP = InputBox("Enter the PC number you wish to run this script on", "Enter the PC number") 

if strIP = "" then 
   wscript.echo "No IP address supplied. Quitting Program"
   wscript.quit
end if

Set objScriptExec = objShell.Exec("ping.exe -n 1 -a " & strIP)
strPingResult = objScriptExec.StdOut.ReadAll
Set objStdOut = objScriptExec.StdOut

strNoPing = "Request timed out."

arrayPingResult = split(strPingResult, vbcrlf)

strCheck = strComp(arrayPingResult(3), strNoPing, 1)

if strCheck = 1 then
   wscript.echo "PC not on the network. Quitting Program"
   wscript.quit
else
   arrayPCLine = split(arrayPingResult(1), " ")
   wscript.echo arrayPCLine(1)
end if
 
@tootiredtocare

I incorporated your code and it now works!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top