Solved

Help writing a VBScript to ping specific servers and then email results to an email address

Posted on 2007-11-19
7
2,293 Views
Last Modified: 2008-02-01
Hi,

I'm looking to write a (hopefully) simple vbscript that will output the results of a ping command to a command line mailer.

We have a number of servers that are periodically updated and then have a scheduled restart around 11pm. I'd like to schedule this new script to 'ping' a number of specified servers, return a simple 'yes' or 'no' value for each one based on whether the ping was returned, then combine these answers into a single string which I will pass to a command line emailer (BMail) as the subject. I'll then get an email at home telling me that the servers are responding after the reboots.

Now I know there are easier ways of doing this and existing free monitoring tools, but I'm keen to practise my scripting so please humour me!

So for example, there are 3 servers to test:

SERV1
SERV2
SERV3

Assuming that Server 1 and 2 reboot fine, but 3 hangs, the output string I want will be:
SERV1: OK SERV2: OK SERV3: FAIL

I'm fine with basic vbscripting, variables, passing values to a shell command prompt etc, what I really need the help with is translating the results of a ping command into a simple yes/no answer. Is this even possible? Perhaps with error/reutrn codes? Any advice much appreciated!

Thanks
0
Comment
Question by:grjitdept
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:chandru_sol
Comment Utility
0
 
LVL 1

Author Comment

by:grjitdept
Comment Utility
That looks the sort of thing I'm after - though a little more advanced - I think the key in the example you've given me is the WMI Win32_Ping  command. Could you help me simplify this a bit? Let's make it very simple. A single machine, SERV1 needs pinging and the results returning to a variable as either OK or FAIL....
0
 
LVL 12

Expert Comment

by:chandru_sol
Comment Utility
I think if you add a machine in the text box and it will give you the ping results

regards
Chandru
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Accepted Solution

by:
MorDrakka earned 500 total points
Comment Utility
Hi,

I have created a short VBS for you i which I explain how to translate into YES and NO. (Basicly you check for the return value TTL or reply from)

Enter your servers in a textfile named servers.txt in the same path.

Output is written in log.txt in the same path.

How the email this I am leaving up to you for now.

Hope this helps

M

PS  Remeber that somethimes server do respond to ping but not to any other service. To check for this I use the FileSystemObject and check voor availabilty for c$.
SET objFSO = CreateObject("Scripting.FileSystemObject")

SET objInputfile = objFSO.OpenTextFile("servers.txt")

SET objOutputfile = objFSO.CreateTextFile("log.txt", True)
 
 

DO WHILE NOT objInputfile.AtEndOfStream

strComputer = objInputfile.readline

SET objShell = CreateObject("WScript.Shell")

SET objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)

strPingResults = LCase(objExec.StdOut.ReadAll)

IF InStr(strPingResults, "TTL") THEN

	objOutputfile.writeline strComputer & " PING = YES "

ELSE

	objOutputfile.writeline strComputer & " PING = NO"

END IF

LOOP

Open in new window

0
 
LVL 1

Author Comment

by:grjitdept
Comment Utility
Absolutely spot on - and nice touch checking the C share as well - thanks!
0
 
LVL 6

Expert Comment

by:MorDrakka
Comment Utility
Oops small rectification:

Since
strPingResults = LCase(objExec.StdOut.ReadAll)
is Lcase (lowercase)
You if statement should be
IF InStr(strPingResults, "ttl") THEN

THanks!
0
 
LVL 1

Author Comment

by:grjitdept
Comment Utility
This didn't work first time I tried - seemed to be because you used the LCase function on the ping results but then checked for TTL in uppercase? I changed the TTL string to ttl and it seemed to work then.

You mentioned checking for availability of the C$ share. Any chance you could show me how to do that within this script? i.e. check ping AND check c$ share?

Thanks again
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now