Solved

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

Posted on 2007-11-19
7
2,304 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 6

Accepted Solution

by:
MorDrakka earned 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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