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.
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.
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 ===
This page was last modified on 10-11-26