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

help writing a script or batch to test ping 2

Status
Not open for further replies.

lpblauen

Technical User
Dec 2, 2004
193
US
I'm stuck. I need to ping 20 or so systems with a ping and if it fails to tell me which system fails the ping test. Can this be done within a batch script or vbs? if so how? i would like to run this everyday to verify that all the systems are still working with out having to go to each one everyday.
 
try this. you may have to edit it for syntax.


strMachines = ""atl-dc-01;atl-win2k-01;atl-nt4-01;atl-dc-02""
aMachines = split(strMachines, "";"")

For Each machine in aMachines
Set objPing = GetObject(""winmgmts:{impersonationLevel=impersonate}"")._
ExecQuery(""select * from Win32_PingStatus where address = '""_
& machine & ""'"")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
WScript.Echo(""Computer "" & machine & "" is not reachable"")
End If
Next
Next"
 
Is this batch or vbs. I tried both extentions and both fail.
Here are the results as cmd and vbs. As a vbs I get a popup error expected end of staement compilation error??

C:\>strMachines = ""zest;systrain;manx;lynx""
'strMachines' is not recognized as an internal or external command,
operable program or batch file.

C:\>aMachines = split(strMachines, "";"")
'aMachines' is not recognized as an internal or external command,
operable program or batch file.
Each was unexpected at this time.
C:\>For Each machine in aMachines
C:\>ren test.cmd test.vbs

C:\>test

C:\>
 
That was a VB script, and lets wait for Jessica to tell you why it did not work.

As for a Batch file, depending if you know all the IP addresses of the 20 machines or not it will be a little different, also if the addresses are continuous or they are scattered.
i.e. x.x.x.1, x.x.x.12, x.x.x.20, x.x.x.22 etc...

For the 20 uncontiguous addresses that are known tha batch file would look like:
Code:
@ECHO OFF
ping [red]X.X.X.[blue]10[/blue][/red] > pinglog.txt
ping [red]X.X.X.[blue]15[/blue][/red] > pinglog.txt
ping [red]X.X.X.[blue]22[/blue][/red] > pinglog.txt
ping [red]X.X.X.[blue]35[/blue][/red] > pinglog.txt

so on and so forth...

However if the addresses are continuous then you can just use a loop. to cycle through them.

It will output all results to a text file you then only need to check the test file to see wich adress failed if any.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ok as a batch file once I have the pinglog.txt what would the easiest way for it to tell me the failures. I know in unix I could grep the file and just get to failures. How would I do that in wndowsxp.
 
It's a vbs, but you need to get rid of those double-quotes in the script. It works after you do that. So the new .vbs file would be:

strMachines = "computer1;computer2;computer3"
aMachines = split(strMachines, ";")

For Each machine in aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
WScript.Echo("Computer " & machine & " is not reachable")
End If
Next
Next

If you're running this from the command line, you should type:
c:\cscript filename.vbs

I would put a bogus computer in your list just to test that the script will throw the popup if it can't reach the host.
 
That works. I get a popup telling me which system is unreachable.. Thanks
 
Just open the text file and read it, if the Pig failed it will tell you "Request time out" next to the IP it pinged at the moment.

The txt file will look like this:
Code:
[green]? Which address it pinged:[/green]
Pinging 90.0.0.254 with 32 bytes of data:?
[green]Results of the Ping[/green]?
Reply from 90.0.0.254: bytes=32 time=1ms TTL=255?
Reply from 90.0.0.254: bytes=32 time<1ms TTL=255?
Reply from 90.0.0.254: bytes=32 time=1ms TTL=255?
Reply from 90.0.0.254: bytes=32 time<1ms TTL=255?
?[green]Statistics[/green]
Ping statistics for 90.0.0.254:?
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),?
Approximate round trip times in milli-seconds:?
    Minimum = 0ms, Maximum = 1ms, Average = 0ms?
?
Pinging 90.0.0.1 with 32 bytes of data:?
?
Request timed out.?
Request timed out.?
Request timed out.?
Request timed out.?
?

Ping statistics for 90.0.0.1:?
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),?
The address with the request time out will be the one that failed. Just use notepad search feature to find "Request Time out"



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
If you were to use the method Vacunita suggests, which would also work, I would recommend using the command as follows:

ping -n 1 xxx.xxx.xxx.xxx

The -n 1 sends a single echo request instead of the default four, which would make your file a little easier to read.

You also need double redirects after the initial one when outputting (>>) otherwise each succeeding line overwrites the previous output.
 
the double double qoutes is mistake on my part. the editor sometimes puts ' ' for " or "" or "
copy this and paste it to notepad and save it as whatever.vbs
to invoke it, you can double click on it.
you can also have the machine names in a text file, but you have to code for reading the file and feeding it to the loop:

'here are the machines you need to ping (computernames)
strMachines = "pc1;pc2;pc3;pc4"
'split each machine name
aMachines = split(strMachines, ";")

For Each machine in aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
WScript.Echo("Computer " & machine & " is not reachable")
End If
Next
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top