This can be helpful if you want to run a persistent ping to a host, and log the results to a file to be reviewed later. There are two methods:
LOG ONLY PING FAILURES – This method will log only the failures or timed-out ping requests and their times. Cut and paste the below script into a txt doc. Save the text document as ping.vbs on the root of C:\. Open a CMD prompt, cd to the root of C and type: cscript ping.vbs x.x.x.x pinglog.txt
hostIp = wscript.arguments(0) logfilename = wscript.arguments(1) Set fso = CreateObject("Scripting.FileSystemObject") Set Shell = CreateObject("Wscript.Shell") ' OpenTextFile Method requires a Const value ' (Over)Write = 2 Append = 8 Set logfile = fso.OpenTextFile(logfilename, 8, True) shellstring = "%comspec% /c ping -t " & hostIP Set oExec = Shell.Exec(shellstring) wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt" Do While oExec.StdOut.AtEndOfStream <> True pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine If InStr(pingline, "TTL=") = 0 Then logfile.WriteLine(pingline) End If Loop
LOG PING SUCCESS AND FAILURES– This method will produce an enormous log if left running over an extending period of time. Instead of capturing times of failures only, this will log each successful ping also. Cut and paste the below script into a txt doc. Save the text document as ping.vbs on the root of C:\. Open a CMD prompt, cd to the root of C and type: cscript ping.vbs x.x.x.x pinglog.txt
hostIp = wscript.arguments(0) logfilename = wscript.arguments(1) Set fso = CreateObject("Scripting.FileSystemObject") Set Shell = CreateObject("Wscript.Shell") ' OpenTextFile Method requires a Const value ' (Over)Write = 2 Append = 8 Set logfile = fso.OpenTextFile(logfilename, 8, True) shellstring = "%comspec% /c ping -t " & hostIP Set oExec = Shell.Exec(shellstring) wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt" Do While oExec.StdOut.AtEndOfStream <> True pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine If InStr(pingline, "TTL=") - 128 Then logfile.WriteLine(pingline) End If Loop