Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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,323 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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