Continuous Ping

Sometimes when testing network connectivity to a server on the local LAN or a web site I use the ubiquitous PING command to do the job.  If I need to do it repeatedly I tend to write a quick DOS batch file (a text based script with a file extension of BAT or CMD).

For example this script would repeatedly ping localhost - the name used for the machine the script is being run on.

@echo off
:START
Ping -n 1 localhost
Goto START

A few years ago I even extended this to include a ping count but it didn’t really add much. I decided it would be more useful to have something that at least gave an indication of how many “good and bad” pings had occurred. At the same time I also found that I wasn’t always necessarily at my computer as I might be tinkering with cables and other networking gear so having some sort of audible cue as to how the pings were going.

I put together the following VBScript to do just this. It hasn’t been tested extensively and I’m sure that there could be a bit more validation. When the script is run it asks for the host to ping and a time at which to stop pinging. It then opens up an Internet Explorer window that displays the information about the pinging.

Option Explicit
 
'Settings:
'- Number of PC speaker beeps on ping successes/failures
Const BEEPS_ON_PING = 1
Const BEEPS_ON_NO_PING = 3
'- Milliseconds of pause between pings
Const PAUSE_MS = 200
'- Milliseconds pause before automatically closing the progress window
Const AUTO_CLOSE = True
Const FINAL_PAUSE = 5000
 
'Variable definitions
Dim strHost
Dim dtEnd
Dim objShell, objExplorer
Dim intSuccessfulPingCount, intUnsuccessfulPingCount
 
'Initialise
Set objExplorer = CreateObject("InternetExplorer.Application")
 
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Width = 400
objExplorer.Height = 400
objExplorer.Document.Body.Style.Cursor = "default"
intSuccessfulPingCount = 0
intUnsuccessfulPingCount = 0
 
 
'If we don't have a host then ask for one
If Wscript.Arguments.Count < 1 Then
    strHost = InputBox("Please enter the host to ping", "Enter Host", "localhost")
Else
    strHost = Wscript.Arguments(0)
End If
 
'Double check what we have
If Len(strHost)  0
    'Carry out the ping
    If Ping(strHost, False) = True Then
        'Ping successful
        intSuccessfulPingCount = intSuccessfulPingCount + 1
        Beep(BEEPS_ON_PING)
        WScript.Sleep(PAUSE_MS)
    Else
        'Ping unsuccessful
        intUnsuccessfulPingCount = intUnsuccessfulPingCount + 1
        Beep(BEEPS_ON_NO_PING)
        WScript.Sleep(PAUSE_MS)
    End If
    UpdatePings "<p><b>RUNNING...</b></p>"
Wend
 
'Finalise
objExplorer.Document.Body.Style.Cursor = "default"
If AUTO_CLOSE Then
    UpdatePings("<p><b>AUTO CLOSING...</b></p>")
    Wscript.Sleep FINAL_PAUSE
    objExplorer.Quit
Else
    UpdatePings("<p><b>PING CYCLE COMPLETED</b></p>")
End If
 
 
'Update the display of pings
Sub UpdatePings(p_strSuffix)
    Dim strDisplay
 
    strDisplay = ""
 
    strDisplay = strDisplay & "<p>"
    strDisplay = strDisplay & "Finish pinging @ "
    strDisplay = strDisplay & dtEnd
    strDisplay = strDisplay & "<br />"
    strDisplay = strDisplay & "Currently ... "
    strDisplay = strDisplay & Now()
    strDisplay = strDisplay & "</p>"
 
    strDisplay = strDisplay & "<hr>"
 
    strDisplay = strDisplay & "<p>"
    strDisplay = strDisplay & "<table border='0'>"
    strDisplay = strDisplay & "<tr>"
    strDisplay = strDisplay & "<td><font color='green'>Successful pings</font></td>"
    strDisplay = strDisplay & "<td><font color='green'>"
    strDisplay = strDisplay & intSuccessfulPingCount
    strDisplay = strDisplay & "</font></td>"
    strDisplay = strDisplay & "</tr>"
    strDisplay = strDisplay & "<tr>"
    strDisplay = strDisplay & "<td><font color='red'>Unsuccessful pings</font></td>"
    strDisplay = strDisplay & "<td><font color='red'>"
    strDisplay = strDisplay & intUnsuccessfulPingCount
    strDisplay = strDisplay & "</font></td>"
    strDisplay = strDisplay & "</tr>"
    strDisplay = strDisplay & "</table>"
    strDisplay = strDisplay & "</p>"
 
    strDisplay = strDisplay & "<hr>"
 
    strDisplay = strDisplay & "<p>"
    strDisplay = strDisplay & "Auto Close Enabled: "
    strDisplay = strDisplay & AUTO_CLOSE
    strDisplay = strDisplay & "</p>"
 
    strDisplay = strDisplay & "<hr>"
 
    strDisplay = strDisplay & p_strSuffix
 
    objExplorer.Document.Body.InnerHTML = strDisplay
End Sub
 
 
'This function pings the specified host
Function Ping(p_strHost, p_boolDisplay)
    Dim objPing, objStatus
 
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_PingStatus where address = '" & p_strHost & "'")
 
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode0 then
            Ping = False
            If p_boolDisplay Then
                WScript.Echo "Status code is " & objStatus.StatusCode
            End If
        Else
            Ping = True
            If p_boolDisplay Then
                Wscript.Echo "Bytes = " & vbTab & objStatus.BufferSize
                Wscript.Echo "Time (ms) = " & vbTab & objStatus.ResponseTime
                Wscript.Echo "TTL (s) = " & vbTab & objStatus.ResponseTimeToLive
            End If
        End if
    Next
End Function
 
 
'Beep the PC speaker a number of times equal to the p_intBeeps parameter
Function Beep(p_intBeeps)
 
    Dim objShell, intCount, strCommand
 
    'Check we have a valid number of beeps not fool proof but it will do for now
    Beep = False
    If IsNumeric(p_intBeeps) Then
        If p_intBeeps > 0 Then
            Beep = True
        End If
    End If
 
    If Beep Then
        'Build the command string to run
        strCommand = "cmd /c echo"
        For intCount = 1 to p_intBeeps
            strCommand = strCommand & " " & chr(007)
        Next
 
        'Run the command in the background
        Set objShell = Wscript.CreateObject("wscript.Shell")
        objShell.Run strCommand, 0
 
        Beep = True
    End If
End Function

There are also a few options in the script to specify numbers of beeps for successful and unsuccessful pings as well as auto closing of the Internet Explorer window and lengths of pauses between pings and before auto closing the window.

Author: Stephen Millard
Tags: | shell | vbs |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read