DavidOverton.com
This site is my way to share my views and general business and IT information with you about Microsoft, IT solutions for ISVs, technologists and businesses, large and small.  
Ping a remote host then if it times out run a TRACERT and send an email

Recently I needed to find out why a connection to a server was failing every so often. I knocked this script up from a few pieces of old wood and a couple of tin cans I had lying around to do just that.

It was nicely indented until I pasted it into here :-)

Run it using the following command line:

cscript <filename.vbs>

Setup a scheduled task to run it when you need to.

' Created by Ian Watkins - Oxbridge Technology Ltd - May 2007

On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
strComputer = "host.domain.com"

vbCRLF = Chr(13) & Chr(10)

strPingStatus = PingStatus(strComputer)
If strPingStatus = "Success" Then
 Wscript.Echo "Success pinging " & strComputer

Else
 
 WshShell.Run "%comspec% /c c:\windows\system32\tracert.exe " & strComputer & " > c:\belfry\testlog.txt" , ,1

 Const ForReading = 1, ForWriting = 2, ForAppending = 8
 Dim fso, f
 
 Set fso = CreateObject("Scripting.FileSystemObject")
 'Open the file for reading
 Set f = fso.OpenTextFile("c:\belfry\testlog.txt", ForReading)
 'The ReadAll method reads the entire file into the variable BodyText


 Set objEmail = CreateObject("CDO.Message")

 objEmail.From = "[email protected]"
 objEmail.To = "[email protected]"
 objEmail.Subject = "Exciting and informative subject line"
 objEmail.Textbody = "Status - " & now() & " - " & strComputer & " - " & strPingStatus & vbCRLF
 objEmail.Textbody = objEmail.Textbody & f.ReadAll
 objEmail.Send
 
 'Close the file
 f.Close
 Set f = Nothing
 Set fso = Nothing


End If

Set WshShell = Nothing


'******************************************************************************

Function PingStatus(strComputer)

    On Error Resume Next
    strWorkstation = "."
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strWorkstation & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function


Posted Wed, Jun 27 2007 9:57 PM by Ian Watkins

(c)David Overton 2006-23