Solved

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

Posted on 2007-11-19
7
2,298 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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 …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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