[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-12
37
Medium Priority
?
596 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:bsharath
  • 19
  • 10
  • 7
  • +1
37 Comments
 
LVL 10

Expert Comment

by:GlobaLevel
ID: 35746063
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

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35746146
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 35746176
But these will not take each and ping with name and ip address to find the ones that has issues
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 24

Expert Comment

by:yo_bee
ID: 35746240
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35746390
Yes its AD.

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35746444
I have a script at work that I will post when I get in within the next hour.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35748945
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.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35751100
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 35751894
No errors and no outputs
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35753125
I will check when I get in.
8:00 am EST
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35753141
Can you post your code?
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35753579
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?
0
 
LVL 11

Author Comment

by:bsharath
ID: 35766493
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35766840
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 35766878
Thanks Rob

In colum C i get this

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

is there any meaning for this
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35766984
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

0
 
LVL 11

Author Comment

by:bsharath
ID: 35767117
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35767133
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35767352
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
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768343
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?
0
 
LVL 11

Author Comment

by:bsharath
ID: 35768354
Yes all computers are public

Yes we do have dns issue but in other hand want to find systems that have the issue
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768390
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?


0
 
LVL 11

Author Comment

by:bsharath
ID: 35768414
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

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768440
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.

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768542
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....

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768558
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.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35768592
@RobSampson
Your script using NSLOOKUP,right?  
If there is a DNS issue whould this work?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35773837
Yes, my script did use NSLookup, and yes, I believe that would fail if there was a DNS issue.  So, use this script to get the output from the Ping command instead.

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 = ResolveIP(strComputer)
		strHostName = ResolveHostName(strIP)
		objOutput.WriteLine """" & strComputer & """,""" & strIP & """,""" & strHostName & """"
	End If
Wend
objOutput.Close
objInput.Close
MsgBox "Done."

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = 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(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "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

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35776460
@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.


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35781790
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.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35781844
@ 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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35781885
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.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35781931
Really.
I did not know that.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35781961
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.
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35893811
Just curious what was the cause of this issue?
0
 
LVL 11

Author Comment

by:bsharath
ID: 35894951
Did not solve the issue but found all machine has has the issue and manually fixed the dns issues with various solutions
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 35895170
Sounds like you have more of a problem on your hands.
Good luck
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question