Link to home
Start Free TrialLog in
Avatar of masterbaker
masterbakerFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of masterbaker

ASKER

Hi Fernando,

Thanks for putting in the time to help here.  I did copy and paste what you posted into a new file (gwcheck.vbs) but I get this error when it runs:

gwcheck.vbs(7, 19) Microsoft VBScript compilation error: Expected end of statement

If this helps, I run this VBscript from within a separate DOS batch file that performs some ping tests and some other stuff.  Here is the actual line that runs this script:

cscript //Nologo gwcheck.vbs

Any ideas?

Thanks!

Jeff
Hi Jeff;

Sorry, the code I provided will work in Visual Basic 2003 or Visual Basic 2005. Seeming that you have posted your question in the VB .Net thread I assumed that you wanted a VB .Net solution.

If you do not have one of those VB compilers you can download a VB 2005 Express compiler from Microsoft for free at

http://www.microsoft.com/products/info/product.aspx?view=22&pcid=b7d186c7-61c7-43c6-9aa1-d56cca1eb008&crumb=catpage&catid=515c9859-958b-4433-b4f9-91f37258ca2f#HowToBuy

Click on "How to Buy" and then click on Download Now.

I do not know VBscript so I would not be able to supply code in it.

Fernando
I do have access to VB 2005 so I'll give that a shot and report back.  Sorry for the confusion on my part!

Jeff
Not a problem. ;=)
Jeff;

The code I sent you was written as a console project. So in VB 2005 when you create a project select the Console project type then copy and past the code I posted. If you run it from the environment the console window will open and close right after it gets the data therefore if you run it from within the development environment then insert this command in the program.

        Else
            Console.WriteLine("*** Media is disconnected ***")
        End If

        Console.ReadLine()    ' <==== Insert this line here

    End Sub

End Module


If you run it from a DOS window no need to insert the line above because the DOS window will stay open.

Fernando
All I can say is....  thanks!!  I am a newbie to VB but I was able to get the project created, your code imported, and the executable successfully built.  I added this to my network test script and it is working perfectly.  This is much better than the error I would get if the media got disconnected.

Thanks again!

Jeff
Not a problem and glad I was able to help. ;=)