Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

How to detect a server outage from a script

Post image for How to detect a server outage from a script

by Steve Wiseman on August 22, 2012 · 5 comments

in Scripts,Tips,Tools,Utility


.

James asked this week:

“Hi Steve. I liked your script that automatically disabled the wireless card when the Ethernet was enabled. But I was wondering, how about a script that ran when a ping to a server timed out? “

This is not as hard as it sounds – we can use the ping command to do most of our work.

We can call ping from VB Script using these commands:

Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("ping -n 1 -w 2000 {HOST}")

See, when we call ping from the command line it will give us a consistent output when the host is reachable:

Ping Output

If the host is up and running, it will always have the text “Reply From..” somewhere in the output.

Lets go back to those two VB Script lines:

Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("ping -n 1 -w 2000 {HOST}")

We can get the output from stdout, and drop it into a string:

sResults = exec.StdOut.ReadAll

And now, we can just look for the text “reply from”.

If we find it, we set our Boolean variable bSuccess to true:

bSuccess = (InStr(LCase(sResults), "reply from") > 0)

Lets bring it all together into once nice function, that is easy to call from our other scripts:

Function PingTest(sHostName)
 Set shell = CreateObject("WScript.Shell")
 Set exec = shell.Exec("ping -n 1 -w 2000 " & sHostName)
 sResults = LCase(exec.StdOut.ReadAll)
 PingTest = (InStr(sResults, "reply from") > 0)
End Function

PingTest() will take the hostname, and return true if it was pingable, and false if it was not. Using the new function I created a script that will show an alert box if a server goes down

Server Down Oh No

Download it from here PingTest.dat

Make sure to rename it to .vbs after you download it.

The script is just an example, and you may want to expand it further to fit your needs. One big shortcoming is that is terminates after the first outage is detected.

Oh, also…when calling scripts like this from a logon batch file, you want to call it like this:

start cscript.exe {PATH}\PingTest.vbs

That way, the batch file does not get stuck on your script waiting for it to finish (Since it keeps running until it notices the server is down)

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 5 comments… read them below or add one }

1 Markus August 22, 2012 at 9:19 am

Great tip steve. Never knew it was so easy to grab standard out from another app. Will be using this in other scripts that I have.

2 Lucas August 22, 2012 at 5:29 pm

Or you can use the built-in PowerShell command Test-Connection. Use the Quiet parameter to return True or False
For example:
Test-Connection 127.0.0.1 -Quiet
Type: help Test-Connection -full
to show all the parameters available as well as examples and syntax.

3 Steve Wiseman August 22, 2012 at 9:50 pm

Nice. Thanks for the tip Lucas, really appreciate it.

4 Michael Allen August 23, 2012 at 10:54 pm

Ping Info View From Nirsoft.

5 Steve Wiseman August 24, 2012 at 11:09 am

Thanks for the info Michael, I will have to check it out. Good to hear from you again, as always.

Steve

Leave a Comment

Category Links - Windows Forum - Exchange Forum