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

Saving PING results to a file 1

Status
Not open for further replies.

Hellooooooooo

Technical User
Oct 21, 2005
66
GB
Hello,

I'm trying to find an easyway to ping my IP range and save the results into a txt file.

My range starts at 192.168.2.8 and runs through to 192.169.15.10. I've tried to use a script that I already have, but the number of IP o be pinged is to many. I'm sure I have seen VB Script or a bat file that you enter the start and end IP and it pings each address and records if it responded, including its host name and IP.

Has anyone seen anything like this??

Cheers All

Marty
 
Here is a basic script to ping each address in that range of IPs once and report the date and time of the reply. It is designed to be run from the command line, but calling from a batch script would work too.

Code:
Set objShell = CreateObject("WScript.Shell")

For i = 168 to 169
    For j = 1 to 254
       For k = 1 to 254
           Set objWshScriptExec = objShell.Exec("ping -n 1 192." & i & "." & j & "." & k)

           Set objStdOut = objWshScriptExec.StdOut

           Do Until objStdOut.AtEndOfStream
               strLine = objStdOut.ReadLine
               If left(strLine,4) = "Repl" Then
                   WScript.Echo Now & " -- " & strLine
               End If
           Loop
       Next
    Next
Next

If you save this as, for example, ping.vbs you could call it from shell like this:

Code:
C:\>cscript ping.vbs > ping_res.txt

And then ping_res.txt would have your results. There is also a way to do this with a batch script, but I found this one on my machine first and this is a VBScript board. By the way, this will take a long time to run. Ping is not a fast command, especially if there are gaps in your scheme as you have to wait for it to time out.
 
Hello,

That's what I've been looking for. It does the trick. I've tried to change the ping command to tracert so that I can see the IP and hostname, but all the txt file contains is the logo for MS Script host. No information. Anyideas.

Cheers

Marty
 
If you like the approach jet042 has posted, you cannot simply replace ping by tracert to get anything out, in particular the host name. Apart from that, you can as well simply stay with the ping which provide you with the host name using -a switch. Having said that, you need further work on instructing the script to turn out the info you want to store for brevity.

These are a couple of modifications you need to implement.

[1] The switch "-l 32" is the default. It is explicitly put for consistency and a little of rebustness concern.
>[tt]Set objWshScriptExec = objShell.Exec("ping -n 1 192." & i & "." & j & "." & k)[/tt]
Replace it by this.
[tt]Set objWshScriptExec = objShell.Exec("ping -n 1[blue] -a -l 32[/blue] 192." & i & "." & j & "." & k)[/tt]

[2] Some non-generic string extraction depending on one's preference and at the mercy of constancy of ping.exe implementation.
[tt]
Do Until objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
[blue]If left(strLine,4) = "Ping" Then
s=replace(strLine,"with 32 bytes of data:" & vbcr,"--")
End If[/blue]
If left(strLine,4) = "Repl" Then
WScript.Echo Now & " -- " & [blue]s & " " & [/blue]strLine
End If
Loop
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top