networking

Ping with a script

I was asked to make a script for monitoring and troubleshooting a network with communication issues. A script that would continuously ping a host server and write the timeouts to a file. I cannot script on my own though, so I went for professor Google, and I found something I want to keep for the future.

Salmon Trout posted it here

I’ll be posting 3 variants of the script, and for the below examples of the output files I have used two virtual servers. One has the IP address 192.168.1.101, which I am pinging from, and the other 192.168.1.105, which I am pinging to. I simply disabled the NIC on 192.168.1.105 to simulate timeouts.

Script 1

This script will continuously ping an end device, and will output every successful and not successful reply to a file.

hostIp      = wscript.arguments(0)
logfilename = wscript.arguments(1)
Set fso     = CreateObject(“Scripting.FileSystemObject”)
Set Shell   = CreateObject(“Wscript.Shell”)
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 Not InStr(pingline, “TTL=”) Then
            logfile.WriteLine(pingline)
        End If
Loop

Example:

24-01-2017 18:17:11
24-01-2017 18:17:11 Pinging 192.168.1.105 with 32 bytes of data:
24-01-2017 18:17:11 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:12 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:13 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:14 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:15 Request timed out.
24-01-2017 18:17:16 Request timed out.
24-01-2017 18:17:17 Request timed out.
24-01-2017 18:17:18 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:19 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:20 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:21 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:17:22 Ping statistics for 192.168.1.105:
24-01-2017 18:17:23     Packets: Sent = 26, Received = 23, Lost = 3 (11% loss),
24-01-2017 18:17:28 Approximate round trip times in milli-seconds:
24-01-2017 18:17:33     Minimum = 0ms, Maximum = 0ms, Average = 0ms
24-01-2017 18:17:38 Control-C

Script 2

This script will continuously ping an end device, and will output only failed replies to a text file. This will conserve disk space.

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

Example:

24-01-2017 18:29:44
24-01-2017 18:29:44 Pinging 192.168.1.105 with 32 bytes of data:
24-01-2017 18:30:03 Request timed out.
24-01-2017 18:30:08 Request timed out.
24-01-2017 18:30:13 Request timed out.
24-01-2017 18:30:23
24-01-2017 18:30:23 Ping statistics for 192.168.1.105:
24-01-2017 18:30:23     Packets: Sent = 27, Received = 24, Lost = 3 (11% loss),
24-01-2017 18:30:23 Approximate round trip times in milli-seconds:
24-01-2017 18:30:23     Minimum = 0ms, Maximum = 0ms, Average = 0ms
24-01-2017 18:30:23 Control-C

Script 3

This script will continuously ping an end device, and will output all failed replies, and successful replies once every minute. This will conserve disk space.

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)
dateminuteOLD = “xx”
dateminuteNEW = “xx”
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
pingline = Date & ” ” & Time & ” ” & oExec.StdOut.ReadLine
dateminuteNEW = left(pingline, 16)

    if dateminuteNEW <> dateminuteOLD then
        logfile.WriteLine(pingline)
    End if
dateminuteOLD = dateminuteNEW

Loop

Example:

24-01-2017 13:30:31
24-01-2017 13:30:31 Pinging 192.168.1.105 with 32 bytes of data:
24-01-2017 13:31:00 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 13:31:08 Ping statistics for 192.168.1.105:
24-01-2017 13:31:08 Approximate round trip times in milli-seconds:
24-01-2017 13:31:08 Control-C
24-01-2017 18:32:52
24-01-2017 18:32:52 Pinging 192.168.1.105 with 32 bytes of data:
24-01-2017 18:33:00 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:34:01 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:35:00 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:35:49 Request timed out.
24-01-2017 18:35:59 Request timed out.
24-01-2017 18:36:02 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:37:01 Reply from 192.168.1.105: bytes=32 time<1ms TTL=128
24-01-2017 18:37:39
24-01-2017 18:37:39     Packets: Sent = 270, Received = 267, Lost = 3 (1% loss),
24-01-2017 18:37:39     Minimum = 0ms, Maximum = 2647ms, Average = 9ms

How to use the script

Copy the script content into notepad, and save it as pingtest3.vbs in a folder eg. c:\Pingtest

1

2

Open Notepad and type c:\windows\system32\cscript.exe C:\Pingtest\pingtest3.vbs 192.168.1.105 pingtest3.txt into the file, and then save it as Scriptcall3.bat in the same folder as the script. It is in this argument you define the name of the output file and which host you want to ping, and not in the script itself.

3

4

Now, simply execute Scriptcall3.bat to run the script.

5