?
Solved

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

Posted on 2007-11-19
7
Medium Priority
?
2,321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20312592
0
 
LVL 1

Author Comment

by:grjitdept
ID: 20312666
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
ID: 20312689
I think if you add a machine in the text box and it will give you the ping results

regards
Chandru
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
MorDrakka earned 2000 total points
ID: 20312692
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
ID: 20312782
Absolutely spot on - and nice touch checking the C share as well - thanks!
0
 
LVL 6

Expert Comment

by:MorDrakka
ID: 20312850
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
ID: 20312936
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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