Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Ping machines in a txt file and save results in 2 ways.

Hi,

Ping machines in a txt file and save results in 2 ways.
I will have machine names in the txt file
Need to ping and get the ip address and then ping ip with -a and get the machine name

reason is many machines show wrong machine names when pinged so need this report.

Any help is great use

regards
raja
Avatar of GlobaLevel
GlobaLevel
Flag of United States of America image

Some vbscripts:
The attached script found online


http://www.wisesoft.co.uk/scripts/vbscript_ping_all_computers_in_the_domain.aspx
"John Lumadue" <jlumadue@xxxxxxxxxxx> wrote in message 
news:u56aHWXrFHA.1236@xxxxxxxxxxxxxxxxxxxxxxx 
> This is one of Microsoft's examples to ping multiple machines via an 
> Array(): 
> 
> On Error Resume Next 
> 
> arrTargets = Array("pcname1", "pcname2") 
> For Each strTarget In arrTargets 
> Set objShell = CreateObject("WScript.Shell") 
> Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget) 
> strPingResults = LCase(objExec.StdOut.ReadAll) 
> If InStr(strPingResults, "reply from") Then 
> WScript.Echo VbCrLf & strTarget & " responded to ping." 
> Set objWMIService = GetObject("winmgmts:" _ 
> & "{impersonationLevel=impersonate}!\\" & strTarget & "\root\cimv2") 
> Set colCompSystems = objWMIService.ExecQuery("SELECT * FROM " & _ 
> "Win32_ComputerSystem") 
> For Each objCompSystem In colCompSystems 
> WScript.Echo "Host Name: " & LCase(objCompSystem.Name) 
> Next 
> Else 
> WScript.Echo VbCrLf & strTarget & " did not respond to ping." 
> End If 
> Next 
> 
> 
> The problem I have is that I have over 450 machines to resolve and I am not 
> about to add each one in the Array, separated by quotes. Is there any way 
> of having this script use a text file with the machine names listed instead? 
> It is much easier to paste machines into a text file anytime I want to ping 
> unknown hosts. 
> 
> Thanks! 
> 
> John 


This sample script should help you: 
http://www.microsoft.com/technet/scriptcenter/scripts/misc/input/msinvb03.mspx

The script reads each line of a text file and then adds the line to a Dictionary 
(similar to a collection). Then it loops through each item in the Dictionary.
In the sample script, just replace these 3 lines with your script: 

Set colServices = GetObject("winmgmts://" & objDictionary.Item(objItem) _ 
& "").ExecQuery("Select * from Win32_Service") 
Wscript.Echo colServices.Count 


I haven't tested this, but the 2 scripts combined together would look something 
like this: 


On Error Resume Next 
Const ForReading = 1 
Set objArgs = WScript.Arguments 

Set objDictionary = CreateObject("Scripting.Dictionary") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading) 
i = 0 

Do While objTextFile.AtEndOfStream <> True 
strNextLine = objTextFile.Readline 
objDictionary.Add i, strNextLine 
i = i + 1 
Loop 

For Each objItem in objDictionary 
strTarget = objDictionary.Item(objItem) 
Set objShell = CreateObject("WScript.Shell") 
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget) 
strPingResults = LCase(objExec.StdOut.ReadAll) 
If InStr(strPingResults, "reply from") Then 
WScript.Echo VbCrLf & strTarget & " responded to ping." 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strTarget & "\root\cimv2") 
Set colCompSystems = objWMIService.ExecQuery("SELECT * FROM " & _ 
"Win32_ComputerSystem") 
For Each objCompSystem In colCompSystems 
WScript.Echo "Host Name: " & LCase(objCompSystem.Name) 
Next 
Else 
WScript.Echo VbCrLf & strTarget & " did not respond to ping." 
End If 
Next 




. 

Follow-Ups:
Re: How to ping multiple machines from txt file?
From: John Lumadue

Open in new window

Avatar of yo_bee
Have you checked your DNS for any issues first.
This sounds like an DNS issue to me.

He is was script you can use to collect the info you are talking about.  It will need to be modified according to your ip range.
on error resume next
dim i 

For i = 1 to 254
PingHost
Wscript.echo "192.168.0." & i & vbtab & Pinghost
Next

Function PingHost
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping 192.168.0." & i & " -a")
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then

PingHost = True
Else
 
	PingHost = False
End If
end Function

Open in new window

Avatar of bsharath

ASKER

But these will not take each and ping with name and ip address to find the ones that has issues
are you using AD?
If so I an a script that will need to be modified for your domain, but it collects details that you are looking for.
Yes its AD.

I have a script at work that I will post when I get in within the next hour.
Here is my code that will return ip to Computer name.
If may take some time to run through depending on the number of computers.
I set i = a very small range for testing.
If you want it to output to a test file (Tab delimated) run it from the command prompt
cscript "vbs file name.vbs  " > .\outputfile.csv
on error resume next

Dim StrComputerName, StrUser, strManufacturer, StrModel, StrSN
'***************************
'Change your range here
'***************************
For i = 199 to 200
'***************************
'Change your subnet here
'***************************
strname = "192.168.11." & i


	blnPing = PingHost
  	If blnPing = True Then
end if
DataCollection
next
'************************************************************************************
'Data Collection																	*
'************************************************************************************
Function DataCollection
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
 & strName & "\root\cimv2")
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystem In colSystems
	
		strComputerName = objSystem.name
		
        next
	

wscript.echo Strname & vbtab & StrCOmputerName
End Function

'************************************************************************************
'Ping Computer																	*
'************************************************************************************
Function PingHost

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strName)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
	PingHost = True
Else
	PingHost = False
End If

End Function
'************************************************************************

Open in new window

let me know how it goes and if you have any questions.
I would like to post an edit to the code:
Original
	blnPing = PingHost
  	If blnPing = True Then
end if
DataCollection

Open in new window

Edit
	blnPing = PingHost
  	If blnPing = True Then
                        DataCollection
end if

Open in new window

No errors and no outputs
I will check when I get in.
8:00 am EST
Can you post your code?
I just tested this against my network and it worked.
Can try running it without the Output parameter and see if you get results.

Can you post your version of the code?
Sorry but does not show both

I will have machine names. It has to ping machine name and get the ip and then ping the retrieved ip and then get machine name.
See how this code goes.

Regards,

Rob.
strComputers = "computers.txt"
strOutput = "results.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strComputers, 1, False)
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """Computer"",""Resolved IP"",""Resolved Host Name"""
While Not objInput.AtEndOfStream
	strComputer = objInput.ReadLine
	If Trim(strComputer) <> "" Then
		strIP = ResolveIPNSLookup(strComputer)
		strHostName = ResolveHostName(strIP)
		objOutput.WriteLine """" & strComputer & """,""" & strIP & """,""" & strHostName & """"
	End If
Wend
objOutput.Close
objInput.Close
MsgBox "Done."

Function ResolveIPNSLookup(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("nslookup " & computerName)
	strPingOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
	RegEx.Global = True
	If RegEx.Test(strPingOutput) Then
		If RegEx.Execute(strPingOutput).Count > 1 Then
			ResolveIPNSLookup = RegEx.Execute(strPingOutput)(1)
		Else
			ResolveIPNSLookup = "IP Address could not be resolved"
		End If
	Else
		ResolveIPNSLookup = "IP Address could not be resolved"
	End If
End Function

Function ResolveHostName(strIPAddress)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("nbtstat -a " & strIPAddress)
	strHNOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = ".*<00>.*UNIQUE.*"
	RegEx.Global = True
	If RegEx.Test(strHNOutput) Then
		strText = Trim(RegEx.Execute(strHNOutput)(0))
		ResolveHostName = Left(strText, InStr(strText, " ") - 1)
	Else
		ResolveHostName = "Hostname could not be resolved"
	End If
End Function

Open in new window

Thanks Rob

In colum C i get this

Machine name <00>
At the end get <00>

is there any meaning for this
It's part of the nbtstat output.  Try this to get rid of that.

Regards,

Rob.
strComputers = "computers.txt"
strOutput = "results.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strComputers, 1, False)
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """Computer"",""Resolved IP"",""Resolved Host Name"""
While Not objInput.AtEndOfStream
	strComputer = objInput.ReadLine
	If Trim(strComputer) <> "" Then
		strIP = ResolveIPNSLookup(strComputer)
		strHostName = ResolveHostName(strIP)
		objOutput.WriteLine """" & strComputer & """,""" & strIP & """,""" & strHostName & """"
	End If
Wend
objOutput.Close
objInput.Close
MsgBox "Done."

Function ResolveIPNSLookup(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("nslookup " & computerName)
	strPingOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
	RegEx.Global = True
	If RegEx.Test(strPingOutput) Then
		If RegEx.Execute(strPingOutput).Count > 1 Then
			ResolveIPNSLookup = RegEx.Execute(strPingOutput)(1)
		Else
			ResolveIPNSLookup = "IP Address could not be resolved"
		End If
	Else
		ResolveIPNSLookup = "IP Address could not be resolved"
	End If
End Function

Function ResolveHostName(strIPAddress)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("nbtstat -a " & strIPAddress)
	strHNOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = ".*<00>.*UNIQUE.*"
	RegEx.Global = True
	If RegEx.Test(strHNOutput) Then
		strText = Trim(RegEx.Execute(strHNOutput)(0))
		strText = Left(strText, InStr(strText, " ") - 1)
		If InStr(strText, "<") > 0 Then strText = Left(strText, InStr(strText, "<") - 1)
		ResolveHostName = strText
	Else
		ResolveHostName = "Hostname could not be resolved"
	End If
End Function

Open in new window

Rob
another issue
I get this for many
But when i ping manually it shows the ip address
IP Address could not be resolved      Hostname could not be resolved
The IP Address uses nslookup

For one of those, if you manually do
nslookup YourComputerName

the last IP address in the output should be the IP address.

Rob.
I get this

C:\>nslookup M-XP043
Server:  inad.dev.co.uk
Address:  129.110.110.125

DNS request timed out.
    timeout was 2 seconds.
*** Request to inad.co.uk timed-out

But when i ping i get the ip and the machine is live
I am confused
What went wrong with my script?

Your issue really sounds that it is a DNS issue.
Have you confirmed that replication is working properly?
Is salvaging setup on DNS?

One other thing.  Your ip-address looks like it's a public class and not 10. , 172. Or 192.168.  

All your computers are public?
Yes all computers are public

Yes we do have dns issue but in other hand want to find systems that have the issue
How many computers are you dealing with in total?
How many are ones in question so far?
How many dns servers?
Do you know if salvaging is enabled on the DNS servers?
Have you run any diags on the dns replication?


How many computers are you dealing with in total?
2000
How many are ones in question so far?

How many dns servers?
2
Do you know if salvaging is enabled on the DNS servers?
Its enabled
Have you run any diags on the dns replication?
No

So what I would do next is compare the 2 dns servers records. They should match if all 2000 machines should be talking to both servers for resolution.
Then I would make sure that these two servers can stay in sync. If your servers are not replicating to each other this could be your root cause.
Are your dhcp managed by you or hosted?
If you have access to the dhcp servers you can also compare the records with your dns records.

Also if possible can you give a discription of your topology?
You mentioned that you are running in a AD environment.
Are the DNS Services hosted by the AD's or are they standalones?

i.e. 2 AD (Running DHCP, DNS, IIS,etc...,) , Subnets,  etc....

Please take a moment to respond  
    Did this comment work for you?  Yes  Partially  No Was the comment complete?  Yes  Partially  No Was the comment easy to understand?  Yes  Partially  No Overall, how would you rate this comment?  Excellent  Good  Average  SubmitCancel  
   

 ID:35766493Author:bsharathDate:05/15/11 10:14 PMAuthor Comment  

Was this comment helpful? Yes No
bsharath:
Sorry but does not show both

I will have machine names. It has to ping machine name and get the ip and then ping the retrieved ip and then get machine name.

I am confused you are trying to get a list of ip --> computer names.
My script will ping a ip-address then connect to that computer via ip-address and return the name of that computer.

Are you able to even connect to these computer WMI ?
If not then my script is useless.
@RobSampson
Your script using NSLOOKUP,right?  
If there is a DNS issue whould this work?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@rob

Your script seems to  use DNS to resolve an IP-Address, is that correct?
If so think that is "bsharath" real issue.
He would get a false positive on Name to IP.


Technically yes, even the ping would use a DNS cache, so it could still return false positives.  This seems like an odd way to try to fix DNS issues.

Rob.
@ Rob

I agree.
That is why my script will at least give you the proper IP to Computer since it is not depending on any DNS info at all.
Strictly ping an IP and if there is a response connect remotely to the WMI and collect the computer name then spit it to the output.
in addition the network is 100% public which is strange also.
Yeah, a problem with that though is that the WMI connection can fail anyway if there *is* a DNS issue....

It's a difficult one....

Rob.
Really.
I did not know that.
Yeah, I've seen it happen a few times with some stale DNS records after a PC has not been on for the expiration time of my DHCP reservation.  It doesn't happen often, but if you've got DNS issues, then it potentially could.

Rob.
Just curious what was the cause of this issue?
Did not solve the issue but found all machine has has the issue and manually fixed the dns issues with various solutions
Sounds like you have more of a problem on your hands.
Good luck