Question : Script to read and ping default gateway needs an enhancement

Hi all,

I have taken a script I found out on the Internet somewhere and hacked it up to do what I needed for part of an overall "network health check".  What this script should do is grab my default gateway, try to ping it, and let me know the results.  The script is posted below.  

My problem is that when the network connection is disconnected, the script fails on this line:

Set objExec = objShell.Exec("ping " & strTarget(1))

How can I enhance this script to do one of these two things (#1 being the preferred):

#1: Report a third message that the media is disconnected

#2: Report the same "Default Gateway is DOWN" message

Thanks!

--------------------------------------------------------

Dim oShell
Dim oShellExec, oStdOutputText,sText, strTarget, sCMD
sCMD = "%comspec% /c route print"
Set oShell = CreateObject("Wscript.Shell")
Set oShellExec = oShell.Exec(sCMD)
set oStdOutputText = oShellExec.StdOut
Do While Not oStdOutputText.AtEndOfStream
sText = oStdOutputText.ReadLine
If InStr(sText, "Default Gateway") <> 0 Then
strTarget = split(sText,":")
exit do
End If
Loop

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping " & strTarget(1))
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
  WScript.Echo "Local router is UP"
Else
  WScript.Echo "******** Local router is DOWN ********"
End If

Answer : Script to read and ping default gateway needs an enhancement

Hi masterbaker;

This should give you what you want.

Imports System.Text.RegularExpressions
Imports System.IO

Module Module1

    Sub Main()
        Dim proc1 As New Process
        proc1.StartInfo.FileName = "cmd"
        proc1.StartInfo.Arguments = "%comspec% /c route print"
        proc1.StartInfo.RedirectStandardOutput = True
        proc1.StartInfo.UseShellExecute = False
        proc1.StartInfo.CreateNoWindow = True
        proc1.Start()
        Dim sr1 As StreamReader = proc1.StandardOutput
        Dim output1 As String = sr1.ReadToEnd()

        proc1.WaitForExit()

        Dim m As Match = Regex.Match(output1, _
            "Default\s+Gateway:\s+(\d+\.\d+\.\d+\.\d+)", RegexOptions.IgnoreCase)
        Dim Gateway As String
        If m.Success Then
            Gateway = m.Groups(1).Value
            Dim proc2 As New Process
            proc2.StartInfo.FileName = "ping"
            proc2.StartInfo.Arguments = Gateway
            proc2.StartInfo.RedirectStandardOutput = True
            proc2.StartInfo.UseShellExecute = False
            proc2.StartInfo.CreateNoWindow = True
            proc2.Start()
            Dim sr2 As StreamReader = proc2.StandardOutput
            Dim output2 As String = sr2.ReadToEnd()

            proc2.WaitForExit()

            If Regex.IsMatch(output2, "reply\s+from", RegexOptions.IgnoreCase) Then
                Console.WriteLine("Local router " & Gateway & " is UP")
            Else
                Console.WriteLine("******** Local router is DOWN ********")
            End If
        Else
            Console.WriteLine("*** Media is disconnected ***")
        End If

    End Sub

End Module


Fernando
Random Solutions  
 
programming4us programming4us