Perform actions if device starts or stops pinging [Script]

The below VBS script can be used to perform an action after a computer or device either starts or stops responding to pings.

Usage:

Call the function WaitForPing(SuccessOrFailure, “Name or IP”, TIMEOUT_PING_SUCCESS)

Set SuccessOrFailure to TRUE to wait for the computer to start pinging

Set SuccessOrFailure to FALSE to wait for the computer to stop pinging

Set the Const TIMEOUT_PING_SUCCESS to how long you want to wait, there is a in-built limit of 300 seconds increase this if you need.

Usage Example 1 – Wait for a computer to start pinging:

if WaitForPing(True, "192.168.100.1", TIMEOUT_PING_SUCCESS ) = 1 Then
	WScript.Echo "I can now ping it :-)"
Else
	WScript.Echo "Failed to ping it after " & TIMEOUT_PING_SUCCESS & " seconds"
End if

Usage Example 2 – Wait for a computer to stop pinging

if WaitForPing(False, "192.168.100.1", TIMEOUT_PING_SUCCESS ) = 1 Then
	WScript.Echo "I can't ping it anymore :-)"
Else
	WScript.Echo "Still pinging after " & TIMEOUT_PING_SUCCESS & " seconds"
End if

The Script:

Const TIMEOUT_PING_SUCCESS = 60  'number of seconds to wait for a successful ping
 
'USAGE:
' WaitForPing(SuccessOrFailure, "Name or IP", TIMEOUT_PING_SUCCESS)
' Set SuccessOrFailure to TRUE to wait for the computer to start pinging
' Set SuccessOrFailure to FALSE to wait for the computer to stop pinging
 
'Example:
if WaitForPing(True, "192.168.100.1", TIMEOUT_PING_SUCCESS ) = 1 Then
	WScript.Echo "I can now ping it :-)"
Else
	WScript.Echo "Failed to ping it after " & TIMEOUT_PING_SUCCESS & " seconds"
End if
 
 
' Ping a host by name or IP. Wait for "n" seconds for either a successful ping
'  or an unsuccessful ping.
Function WaitForPing( bSuccessOrFailure, strComputer, dwSeconds )
Dim objPinger, objStatus, dwSecondsElapsed
	if dwSeconds > 300 Then 
		dwSeconds = 300 ' Don't wait forever even if asked
	End If
 
	WaitForPing = -1
	while WaitForPing = -1 
		Set objPinger = WMIQuery( ".", "Select * From Win32_PingStatus where Address = '" & strComputer & "'", "" )
 
		For Each objStatus in objPinger
		    If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 'not successful
				If Not bSuccessOrFailure Then 'Are we waiting for failure
					WaitForPing = 1   'it didn't ping so return
				End If
		    Else 'ping succcessful
				If bSuccessOrFailure Then 'Are we waiting for success
					WaitForPing = 1  'It pinged so return
				End If
		    End If
 
			WScript.Sleep 1000
 
			dwSeconds = (dwSeconds - 1) ' this will guarantee we get out sometime.
 
			if (WaitFOrPing <> 1) And (dwSeconds = 0) Then ' did we time out yet
				WaitForPing = 2 'Timeout waiting for requested ping status.
			End If
		Next
	Wend 
	Set objPinger = Nothing
End Function
 
'Perform a WMI Query with optional security tokens.
' pass strMonikerPriveleges as an empty string or add a comma separated
'      list of security tokens as needed.
Function WMIQuery( strComputer, strQuery, strMonikerPriveleges)
On Error Resume Next
Dim objWMIService,objQuery, strMoniker
	strMoniker = ""
	if Len(strMonikerPriveleges) <> 0 Then
		strMoniker = ",(" & strMonikerPriveleges & ")"
	End If
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate" & strMoniker & "}!\\" & strComputer & "\root\cimv2")
	Abort "WMIQuery-CreatObject'", strComputer
	Set objQuery = objWMIService.ExecQuery(strQuery)
	Abort "WMIQuery-ExecQuery", strQuery
	Set WMIQuery = objQuery	
On Error GoTo 0
End Function

Leave a Comment

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