A VB script to execute a command when a ping fails

Blue Bar separator

The following VBS script will ping "TargetIPAddress" (argument 1) for a "PingCount" (argument 2) of times and if "FailureCount" (argument 3) pings or more are lost it will execute "CommandToExecute" (argument 4). The script will then either exit or continue based on the value of "StopOrContinue" (argument 6). If fewer than "FailureCount" pings are lost or "StopOrContinue" says to continue the script will sleep for "SleepTime" (argument 5) seconds and then start over pinging "TargetIPAddress".

Failures are indicated with a message in the command window that the script is executing in and also written to a log named pt.HH_MM_SS.results.txt where HH is the hour, MM is the minute and SS is the second of when the script was started. There is also a working file named pt.HH_MM_SS.temp that is used to hold the results of the last ping command. Both files are created in the current working directory. Neither file is automagically deleted so if you run this script multiple times these files will build up.

To execute the script open a command window and type cscript ping_trigger.vbs followed by all the arguments. See Examples 1a, 2a, 3a, and 4a.

If "StopOrContinue" is not set to "s" you will have to press control-C to terminate the script.

Example 1a - The command says to ping the host www.cox.net, send 3 pings and trigger if at least 1 is lost. If none are lost sleep for 5 seconds and try again. If any are lost execute the command "" and then continue. Note that the null command is saying don't do anything special, just record the lost pings. The output shows that at 7:21:37 1 ping was lost. This form of the command is useful for creating a reachability log. Note the control-C needed to stop execution.

C:\>cscript ping_trigger.vbs www.cox.net 3 1 5 "" c
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
                                                                                
ping_trigger.vbs www.cox.net 3 1 5 "" c
11/8/2006 7:21:37 PM packets lost <<1>>
^C


Example 1b - Contents of the pt.HH_MM_SS.results.txt file. Note that it has the start date time and the one failure indicating the 1 lost ping packet.
"11/8/2006 7:14:26 PM  ping_trigger.vbs www.cox.net 3 1 5 "" c"

****************************************                                        

11/8/2006 7:21:37 PM packets lost <<1>>



Example 2a - This example is pinging a host across a marginal network. It looses between 1 and 3 packets out of 3 at each iteration.
C:\>cscript ping_trigger.vbs 192.168.1.100 3 1 5 "" c                           
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

ping_trigger.vbs 192.168.1.100 3 1 5 "" c
11/8/2006 7:44:22 PM packets lost <<1>>
11/8/2006 7:44:42 PM packets lost <<2>>
11/8/2006 7:44:56 PM packets lost <<2>>
11/8/2006 7:45:02 PM packets lost <<1>>
11/8/2006 7:45:09 PM packets lost <<1>>
11/8/2006 7:45:25 PM packets lost <<3>>
11/8/2006 7:45:31 PM packets lost <<1>>
11/8/2006 7:45:38 PM packets lost <<1>>
11/8/2006 7:45:49 PM packets lost <<2>>
^C


Example 2b - Contents of the pt.HH_MM_SS.results.txt file.
"11/8/2006 7:44:15 PM  ping_trigger.vbs 192.168.1.100 3 1 5 "" c"               

**************************************** 

11/8/2006 7:44:22 PM packets lost <<1>>

**************************************** 

11/8/2006 7:44:42 PM packets lost <<2>>

**************************************** 

11/8/2006 7:44:56 PM packets lost <<2>>

**************************************** 

11/8/2006 7:45:02 PM packets lost <<1>>

**************************************** 

11/8/2006 7:45:09 PM packets lost <<1>>

**************************************** 

11/8/2006 7:45:25 PM packets lost <<3>>

**************************************** 

11/8/2006 7:45:31 PM packets lost <<1>>

**************************************** 

11/8/2006 7:45:38 PM packets lost <<1>>

**************************************** 

11/8/2006 7:45:49 PM packets lost <<2>>



Reachability logs are useful but there are many tools that can do this better. The utility that this script adds is the ability to execute a specific command.

Example 3a expands on example 1 by executing a tracert when the pings to the www.cox.net fails. The idea is to check to see how far along the network I can actually get. I have set the FailureCount to 1 for this example. In a real debugging session you would probably set both the PingCount and the FailureCount to much larger numbers. Note also that I stop after the first trigger.

C:\>cscript ping_trigger.vbs www.cox.net 3 1 5 "tracert www.c                   
ox.net -d" c
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

ping_trigger.vbs www.cox.net 3 1 5 "tracert www.cox.net -d" s
11/8/2006 8:08:34 PM packets lost <<1>>


Example 3b - Contents of the results.txt file with the date-time of the failed ping and the output from tracert. Note that I have tuncated the output after the 16th hop.
"11/8/2006 8:01:56 PM  ping_trigger.vbs www.cox.net 3 1 5 "tracert www.cox.net -
d" c"

**************************************** 

11/8/2006 8:08:34 PM packets lost <<1>>

Tracing route to www.cox.net [68.99.123.155]
over a maximum of 30 hops:

  1     *     1291 ms   383 ms  68.104.217.33 
  2   483 ms   396 ms   337 ms  10.118.160.1 
  3   416 ms   344 ms   345 ms  68.2.6.105 
  4   437 ms   452 ms   259 ms  68.2.12.90 
  5   415 ms   297 ms   255 ms  68.2.12.9 
  6   380 ms   274 ms   217 ms  68.2.12.5 
  7   385 ms   451 ms   231 ms  68.2.12.1 
  8   359 ms   220 ms   204 ms  68.2.14.13 
  9   424 ms   227 ms   180 ms  68.1.0.168 
 10   392 ms   199 ms   191 ms  68.1.0.177 
 11     *      489 ms   195 ms  68.1.0.143 
 12   485 ms   192 ms     *     68.1.1.195 
 13     *        *        *     
 14     *        *        *     
 15     *        *        *     
 16     *        *        *     
. . .



Example 4a shows that CommandToExecute is not limited to a single command. It can be anything that you can execute from a command line, in this case a BAT file that executes several commands.
C:\>cscript ping_trigger.vbs 192.168.1.100 3 1 5 "debug.bat"                    
s
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

ping_trigger.vbs 192.168.1.100 3 1 5 "debug.bat" s
11/8/2006 8:28:19 PM packets lost <<1>>


Example 4b - In the interest of space I have removed the output from each of the commands in the BAT file. But I left the commands.
"11/8/2006 8:28:07 PM  ping_trigger.vbs 192.168.1.100 3 1 5 "debug.bat" s"

****************************************

11/8/2006 8:28:19 PM packets lost <<1>>

C:\>ipconfig /displaydns
. . .

C:\>arp -a

. . .

C:\>netstat -e

. . .

C:\>netstat -s

. . .



Here is the script.
REM === ping_trigger.vbs begins here === 
REM
REM ping_trigger.vbs
REM Version 1.0 06-11-05
REM Version 1.1 10-11-26  Added disclaimer
REM Noah Davids - ndav1@cox.net
REM
REM pings "TargetIPAddress" (argument 1) for a "PingCount" (argument 2) of times and if 
REM "FailureCount" (argument 3) pings or more are lost it will execute "CommandToExecute" (argument 4).
REM The script will then either exit or continue based on the value of "StopOrContinue" (argument 6).
REM If fewer than "FailureCount" pings are lost or "StopOrContinue" says to continue the script will
REM sleep for "SleepTime" (argument 5) seconds and then start over pinging "TargetIPAddress".
REM
REM Failures are indicated with a message in the command window that the script is executing in and
REM also written to a log named pt.HH_MM_SS.results.txt where HH is the hour, MM is the minute and
REM SS is the second of when the script was started. There is also a working file named
REM pt.HH_MM_SS.temp that is used to hold the results of the last ping command. Both files are created
REM in the current working directory. Neither file is automagically deleted so if you run this script
REM multiple times these files will build up. 
REM
REM This software is provided on an "AS IS" basis, WITHOUT ANY WARRANTY OR ANY SUPPORT OF ANY KIND. 
REM The AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
REM PARTICULAR PURPOSE.  This disclaimer applies, despite any verbal representations of any kind provided
REM by the author or anyone else.
REM
option explicit
dim TargetIPAddress
dim PingCount
dim FailureCount
dim SleepTime
dim CommandToExecute
dim StopOrContinue
dim StartTIme
dim WshShell
dim objFSO
dim objFile
dim message
dim TempPath
dim ResultsPath

const ForReading   = 1
Const ForWriting   = 2
const ForAppending = 8
dim FileText
dim TextPos

if WScript.Arguments.Count <> 6 then
   WScript.Echo "Usage:"
   WScript.Echo vbtab & "cscript ping_trigger.vbs  "
   WScript.Echo vbtab & vbtab & "  "
   WScript.Echo vbtab & vbtab & vbtab & ""
   WScript.Echo
   WScript.Echo vbtab & "target IP address is the address to be pinged" 
   WScript.Echo vbtab & "ping count is the number of times to ping in each loop" 
   WScript.Echo vbtab & "failure count is how many pings need to be lost in order to report it"
   WScript.Echo vbtab & "sleep time is the time to sleep in milliseconds between ping sets"
   WScript.Echo vbtab & "command to execute is a string enclosed in quotes if it has spaces"
   WScript.Echo vbtab & "stop (s) or continue (not s) is the character s or anything else"
   WScript.Echo
else
   Set objFSO = CreateObject ("Scripting.FileSystemObject")

   TargetIPAddress = WScript.Arguments (0)
   PingCount = WScript.Arguments (1)
   FailureCount = WScript.Arguments (2)
   SleepTime = WScript.Arguments (3)
   CommandToExecute = WScript.Arguments (4)
   StopOrContinue = WScript.Arguments (5)
   WScript.Echo "ping_trigger.vbs " & TargetIPAddress  & " " & PingCount & " " & FailureCount & " " &  SleepTime & " """ & CommandToExecute & """ " & StopOrContinue

   StartTime = time
   if (Mid (StartTime, 2, 1) = ":") then
      message = Mid (StartTime, 1, 1) & "_" & Mid (StartTime, 3, 2) & "_" & Mid (StartTime, 6, 2)
   else
      message = Mid (StartTime, 1, 2) & "_" & Mid (StartTime, 4, 2) & "_" & Mid (StartTime, 7, 2)
   end if
   
   TempPath = "pt." & message & ".temp"
   ResultsPath = "pt." & message & ".results.txt"
   
   Set WshShell = WScript.CreateObject ("WScript.Shell")

   message = date & " " & StartTime & "  ping_trigger.vbs " & TargetIPAddress  & " " & PingCount & " " & FailureCount & " " &  SleepTime & " """ & CommandToExecute & """ " & StopOrContinue
   WshShell.Run "Cmd.exe /c echo " & """" & message & """" & "> " & ResultsPath

   Do While (True)
      WshShell.Run "Cmd.exe /c ping " & TargetIPAddress & " -n " & PingCount & " > " & TempPath, 0 , True
      set objFile = objFSO.OpenTextFile (TempPath, ForReading)
      FileText = objFile.ReadAll
      objFile.Close
      TextPos = Instr (FileText, "Lost =")
      if (TextPos = 0) then
         WScript.Echo "Problem with " & TempPath & " file, cant find Lost count - QUITING"
         WScript.Quit
      else
         FileText = Mid (FileText, TextPos + 7)
         TextPos= Instr (FileText, " ")
         FileText = Mid (FileText, 1, TextPos - 1)
         if FileText >= FailureCount then
            message = Now & " packets lost <<" & FileText & ">>"
            WScript.Echo message
            set objFile = objFSO.OpenTextFile (ResultsPath, ForAppending)
            objFile.WriteLine vbCRLF & "**************************************** "
            objFile.WriteLine vbCRLF & message
            objFile.Close
            WshShell.Run "Cmd.exe /c" & """" & CommandToExecute & """" & " >> " & ResultsPath
            if (StopOrContinue = "s") or (StopOrContinue = "S") then
            	WScript.Quit
            end if
         end if
      end if   
      WScript.Sleep SleepTime
   Loop

end if
REM === ping_trigger.vbs end here === 


Blue Bar separator
This page was last modified on 10-11-26
mailbox Send comments and suggestions
to ndav1@cox.net