Ping Host with a Log File

Another potentially useful script here. It pings a host and records the output in a log file. The ping continues until you press Ctrl+C to stop it. This is useful for running in a background monitoring connectivity response times to a host and seeing if there is any network dropout.

Ping is no guarantee of performance, but it does give some idea if you have some underlying network problem over an extended period.

Run from the command line using the following format:

Ping_With_Log.vbs www.google.co.uk C:\GooglePing.txt
'Script to ping a host and record the output to a log file. 
'Craig Tolley
'Version 1.0 - 09/10/2013 - Initial Release
'Version 2.0 - 10/01/2014 - Complete rewrite to make significantly more usable and readable. 

'-------------------  START SCRIPT -------------------

'Check the provided arguments
If WScript.Arguments.Count = 2 Then
	HostToPing = WScript.Arguments(0)
	LogFileName = WScript.Arguments(1)
Else
	Wscript.Echo "Invalid number of arguments detected. " & Chr(13) & Chr(13) & _
				 "Usage: Ping_With_Log.vbs HostToPing LogFileName" & Chr(13) & Chr(13) & _
				 "The host to ping can either be an IP address or resolvable host name" & Chr(13) & Chr(13) & _
				 "The LogFile can either be just a filename, in which case it will be created in the current directory, or a full folder path and file name" & Chr(13) & Chr(13) & _
				 "e.g Ping_With_Log.vbs www.google.co.uk C:\GooglePing.txt"
	Wscript.Quit
End If

'Definitions
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell   = CreateObject("Wscript.Shell")
Set objLogFile = objFSO.OpenTextFile(logfilename, 8, True)

'Run the command
strCommand = "%comspec% /c ping -t " & HostToPing
Set objExec   = objShell.Exec(strCommand)

Wscript.Echo "Ping with Error Log has started. To stop collecting Ping data press Ctrl+C"

'Keep writing the output of the application until Ctrl+C is pressed to cancel the ping
Do While objExec.StdOut.AtEndOfStream <> True
	objLogFile.WriteLine(Date & " " & Time & " " & objExec.StdOut.ReadLine)
Loop

'Open the log file for viewing
strCommand = "notepad.exe " & LogFileName
objShell.Exec(strCommand)

Be the first to like.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.