Continuous PING script that reads a list of IP addresses or DNS names

Hello,

I found this continous PING VB script on Experts Exchange that I really like becasue the output is clean  but I want it modified to be able to read in a file of IP address or DNS names and I don't know how to do it.  

Can someone fix this script so it can read in the file of IP addresses or DNS names?

Thanks,

Capt.
' This runs a continuous ping with date and time output
'
' 4/2/2010 10:37:52 AM -- Pinging 192.168.1.1 with 32 bytes of data:
'
' 4/2/2010 10:37:54 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61

' 4/2/2010 10:37:55 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61  
'
' To run the script type at the command prompt
'         c:> cscript PingIP.vbs > PingIP.txt
'                  
'==============================================================================
Set objShell = CreateObject("WScript.Shell")

Set objWshScriptExec = objShell.Exec("ping 192.168.1.1 -t")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        WScript.Echo Now & " -- " & strLine
    Else
        Wscript.Echo strLine
    End If
Loop

Open in new window

capt_morganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TakedaTCommented:
I answered a question just like this a while ago.  Check this link.

http://www.experts-exchange.com/Programming/Languages/Q_25098413.html
1
capt_morganAuthor Commented:
Takeda,

I saw the link but the script has many revisions and I am not sure which one I would need.  Is there anyway you can modify the script I have attached and add in those parts that would allow it to read in a Excel CVS file or a TXT file?  

This would make it simpler for me to understand.


Thanks.
0
TakedaTCommented:
The one to use would be the one that got the "Accepted Solution" toward the bottom.  All you need to do is make a hosts.txt with your list of objects to ping on seperate lines and put it in the same directory as the vbs file.  

I can modify yours but it will take some time which unfortunately I don't really have right now.  If you can wait until maybe later on, I can try to rewrite yours.  The one that is in that question is actually a pretty useful script.  I use it myself all the time.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

capt_morganAuthor Commented:
Takeda,

I would prefer it if you could fix mine for me.  It just works easier for me.  I don't mind waiting till next week.

Thanks for much for your help on this.

Capt.
0
capt_morganAuthor Commented:
Takeda,

I prefer the output of my script to  the one you wrote.  If you can get my script to read the hosts.txt file like your but output the results the same way I have in my script then this will work great for me.  

Thanks again

Capt.
0
TakedaTCommented:
You do understand that you will have a cmd window open for each computer in the list right?  A long list will fill your screen up.
0
TakedaTCommented:
This is the only way I could get it to work using the exec method.
' This runs a continuous ping with date and time output
'
' 4/2/2010 10:37:52 AM -- Pinging 192.168.1.1 with 32 bytes of data:
'
' 4/2/2010 10:37:54 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61

' 4/2/2010 10:37:55 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61  
'
' To run the script type at the command prompt
'         c:> cscript PingIP.vbs > PingIP.txt
'                  
'==============================================================================
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objComputersFile = objFSO.OpenTextFile (".\Hosts.txt", 1)
strReadFile=objComputersFile.ReadAll()
arrComputers = Split(Replace(strReadFile," ",""),vbCrLf)

Do
	For each strComputer in arrComputers
		Set objPingComputer = objShell.Exec("ping "&strComputer&" -n 1")
		Set objStdOut = objPingComputer.StdOut
    	strLine = objStdOut.ReadAll
   		If Len(strLine) > 2 Then
        	strEcho = strEcho&vbCrLf&Now & " -- " & strLine
    	Else
        	strEcho = strEcho&vbCrLf&strLine
    	End If
	Next
	wscript.echo strEcho
	strEcho=""
Loop

Open in new window

0
TakedaTCommented:
Here is a much more efficient way to do what you are looking for.  It uses WMI to do the ping, thus avoiding the opening of any cmd windows at all.  It has the same output that you were looking for and loops indefinitely.  If you need explaining on the WMI method, here is a link to some documentation.  You can find a lot of documentation by doing a simple search.

http://technet.microsoft.com/en-us/library/ee692586.aspx
On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objComputersFile = objFSO.OpenTextFile (".\Hosts.txt", 1)
strReadFile=objComputersFile.ReadAll()
arrTargets = Split(Replace(strReadFile," ",""),vbCrLf)
strComputer = "."
strTarget = " client1" 'IP address or hostname
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Do
	strOutput=""
	For i=0 to Ubound(arrTargets)
		strOutput=strOutput&PingHost(arrTargets(i))&vbCrLf
	Next
	MsgBox strOutput
Loop

Function PingHost(strTarget)
	Set colPings = objWMIService.ExecQuery _
	("Select * From Win32_PingStatus where Address = '" & strTarget & "'")
	For Each objPing in colPings
		Select case objPing.StatusCode
		Case 0
			PingHost= strTarget&": Reply from " & objPing.Address & ": bytes=" & objPing.BufferSize & " time:" & objPing.ResponseTime & " TTL=" & objPing.TimeToLive
		Case 11002
			PingHost= strTarget&": Destination Net Unreachable."
		Case 11003
			PingHost= strTarget&": Destination Host Unreachable."
		Case 11010
			PingHost= strTarget&": Request Timed Out."		
		Case else
			PingHost= strTarget&": Did not respond to ping."
		End Select
	Next
End Function

Open in new window

0
capt_morganAuthor Commented:
Takeda,

The script brings up a pop-up window that i constanly need to click OK on which is not really what i wanted.  I need it script to output the result to the screen like this from the "hosts.txt' file.

4/2/2010 10:37:52 AM -- Pinging 192.168.1.1 with 32 bytes of data:
4/2/2010 10:37:54 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61
 4/2/2010 10:37:55 AM -- Reply from 192.168.1.1: bytes=32 time=1ms TTL=61  

I then want the result to output to a file as well for later review.  This I can do Ithink by just piping it to a text file when I run the script "pingscript.vbs > pingresult.txt"  

Please see if you can fix it so it does the output inteh manner I described above.
0
TakedaTCommented:
I am a bit confused as to what you want.

You want the script to ping every item in the list.
Then you want it to output to a file those results to a log file.  

At what time do you want it to output to the screen?  And do you still want it to loop over and over?
0
TakedaTCommented:
OK.  I think this is what you want.  You need to use cscript from a cmd to call this file.  If you call just the file, it will output to the cmd window.  If you call it and use > log.txt, it will output into the log file.  You must use cscript though.  If you just type the filename.vbs, it wont work. You need to type :

cscript filename.vbs                  or
cscript filename.vbs > log.txt

I also set it to pause for 1 second in between loops you can change it in line 5.  Just change the "1" to "5" to make it 5 seconds.
On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objComputersFile = objFSO.OpenTextFile (".\Hosts.txt", 1)
iSleepTime="1"&"000"
strReadFile=objComputersFile.ReadAll()
arrTargets = Split(Replace(strReadFile," ",""),vbCrLf)
strComputer = "."
strTarget = " client1" 'IP address or hostname
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Do
        strOutput=""
        For i=0 to Ubound(arrTargets)
                strOutput=strOutput&PingHost(arrTargets(i))&vbCrLf
        Next
        wscript.StdOut.WriteLine strOutput
        wscript.sleep iSleepTime
Loop

Function PingHost(strTarget)
        Set colPings = objWMIService.ExecQuery _
        ("Select * From Win32_PingStatus where Address = '" & strTarget & "'")
        For Each objPing in colPings
                Select case objPing.StatusCode
                Case 0
                        PingHost= Now&vbTab&strTarget&vbTab&": Reply from " & objPing.Address & ": bytes=" & objPing.BufferSize & " time:" & objPing.ResponseTime & " TTL=" & objPing.TimeToLive
                Case 11002
                        PingHost= Now&vbTab&strTarget&vbTab&": Destination Net Unreachable."
                Case 11003
                        PingHost= Now&vbTab&strTarget&vbTab&": Destination Host Unreachable."
                Case 11010
                        PingHost= Now&vbTab&strTarget&vbTab&": Request Timed Out."              
                Case else
                        PingHost= Now&vbTab&strTarget&vbTab&": Did not respond to ping."
                End Select
        Next
End Function

Open in new window

0
capt_morganAuthor Commented:
Takeda,

Its getting better but still needs some work.  I ran the vb script as instructed....

cscript PingTest.vbs > log.txt

and this was in teh output file.      

4/6/2010 1:43:09 PM      192.168.1.1      : Request Timed Out.
4/6/2010 1:43:09 PM      192.168.1.2      : Reply from 192.168.1.2: bytes=32 time:1 TTL=128
4/6/2010 1:43:09 PM      192.168.1.3   : Reply from 192.168.1.3: bytes=32 time:5 TTL=128
4/6/2010 1:43:09 PM      192.168.1.4   : Reply from 192.168.1.4: bytes=32 time:2 TTL=128
4/6/2010 1:43:09 PM            : Reply from : bytes=32 time:0 TTL=128
4/6/2010 1:43:09 PM            : Reply from : bytes=32 time:0 TTL=128

4/6/2010 1:43:09 PM      192.168.1.1      : Request Timed Out.
4/6/2010 1:43:09 PM      192.168.1.2      : Reply from 192.168.1.2: bytes=32 time:1 TTL=128
4/6/2010 1:43:09 PM      192.168.1.3   : Reply from 192.168.1.3: bytes=32 time:5 TTL=128
4/6/2010 1:43:09 PM      192.168.1.4   : Reply from 192.168.1.4: bytes=32 time:2 TTL=128
4/6/2010 1:43:09 PM            : Reply from : bytes=32 time:0 TTL=128
4/6/2010 1:43:09 PM            : Reply from : bytes=32 time:0 TTL=128


The issue I have is that it creates these "dummy" ping reponses with no IP address.  can you remove these "dummy" responses.  Second, it does not output the info to the screen for reatime viewing liek my orginal script did.  Can you fix it so it can output to the screen as well as a file?

Thanks,

Capt.
0
TakedaTCommented:
Could you post your hosts.txt file?
0
TakedaTCommented:
OK.  Fixed the dummy lines.  They were for empty lines in the hosts.txt file.

I also made it so you can see it on screen and log at the same time.  It uses a completely different method of writing, so you need to omit the > in the command.  Other than that, the log should be the same as before.  So at the cmd prompt:

cscript filename.vbs                  or
cscript filename.vbs  log.txt
On Error Resume Next
Const ForReadIng = 1, ForWriting = 2, ForAppending = 8
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objComputersFile = objFSO.OpenTextFile (".\Hosts.txt", 1)
iSleepTime="1"&"000"
strReadFile=objComputersFile.ReadAll()
arrTargets = Split(Replace(strReadFile," ",""),vbCrLf)
strComputer = "."
strTarget = " client1" 'IP address or hostname
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set objArg=Wscript.Arguments
If objArg.Count<1 Then
	bLogit=False
Else
	bLogit=True
	strLogFile=".\"&objArg(0)
End If

Do
        strOutput=""
        For i=0 to Ubound(arrTargets)
        	If arrTargets(i)<>"" then strOutput=strOutput&PingHost(arrTargets(i))&vbCrLf
        Next
        wscript.StdOut.WriteLine strOutput
        If bLogit=True then LogIt(strOutput)

        wscript.sleep iSleepTime
Loop

Function PingHost(strTarget)
        Set colPings = objWMIService.ExecQuery _
        ("Select * From Win32_PingStatus where Address = '" & strTarget & "'")
        For Each objPing in colPings
                Select case objPing.StatusCode
                Case 0
                        PingHost= Now&vbTab&strTarget&vbTab&": Reply from " & objPing.Address & ": bytes=" & objPing.BufferSize & " time:" & objPing.ResponseTime & " TTL=" & objPing.TimeToLive
                Case 11002
                        PingHost= Now&vbTab&strTarget&vbTab&": Destination Net Unreachable."
                Case 11003
                        PingHost= Now&vbTab&strTarget&vbTab&": Destination Host Unreachable."
                Case 11010
                        PingHost= Now&vbTab&strTarget&vbTab&": Request Timed Out."              
                Case else
                        PingHost= Now&vbTab&strTarget&vbTab&": Did not respond to ping."
                End Select
        Next
End Function
Function LogIt(line)
	If not objFSO.FileExists(strLogFile) Then objFSO.CreateTextFile strLogFile
	Set objFile = objFSO.OpenTextFile(strLogFile,ForAppending)
	objFile.Writeline line
	objFile.Close
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
capt_morganAuthor Commented:
Takeda,

It works great now!  It sends the output to the screen and a log file just like I wanted it to.   Thanks so much for making those changes for me.  I intend to use this script alot going forward.  

Thanks,

Capt.
0
capt_morganAuthor Commented:
Takeda is a great programmer.  Kudo's to him for sticking with me on this and making the changes I requested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking Protocols

From novice to tech pro — start learning today.