VBscript: Pinger or Ping servers and alert Sys Admins


I needed simple monitoring tool to ping and send emails when hosts are down. I found few free tools, but I need to build my script so I can use variety of purposes.  Here you go,

‘*******************************************************************************
‘* Script Name: Pinger.vbs
‘* Arguments: ServerName
‘*
‘* This script tries to ping the specified server. If the server is NOT pingable
‘* it will send send email to administrators
‘*
‘* Written by: Anand Venkatachalapathy
‘*******************************************************************************
Dim ServerName
Public MailFrom, MailTo
Public sSMTpServerName

‘* Assign a SMTP server
sSMTPServerName = "smtp.company.com"

‘Enter the correct values for who to alert and from address.
MailFrom = "Administrator@company.com"
‘Enter more addresses with delimited by semicolon below
MailTo = "Sysadmin1@company.com"

ServerName = WScript.Arguments(0)
If IsAlive(ServerName) Then
      WScript.Echo ":-)"
Else
      Notify_Admins ServerName
End If

‘*******************************************************************************
‘* End of Script
‘*******************************************************************************

‘*******************************************************************************
‘* Function: IsAlive
‘* Purpose: Ping the Server and return True if pingable, return False if not
‘* not pingable
‘*******************************************************************************
Function IsAlive(ServerName)
      Set WshShell = CreateObject("WScript.Shell")
      PINGFlag = Not CBool(WshShell.run("ping -n 1 " & ServerName,0,True))
      If PINGFlag = True Then
              ‘Successful ping
              IsAlive = True     
      Else        
              ‘Unsuccessful ping
              IsAlive = False
      End If
End Function

‘*******************************************************************************
‘* Sub-routine: Notify_Admins
‘* Purpose: Send email to system administrators
‘*******************************************************************************
Sub Notify_Admins (ServerName)
        ‘* iMsg – holds CDO.Message object
        ‘* Flds – Enumeration for CDO SMTP object properties
        ‘* iConf – holds CDO.Configuration
        Dim iMsg, Flds, iConf
        ‘* SBody – Email Body message
        Dim SBody
        ‘* Assign cdoSendUsingPort is set to 2, i.e., send using SMTP (25) port
        Const cdoSendUsingPort = 2
        ‘* Create CDO Objects and assign to variables
        Set iMsg = CreateObject("CDO.Message")
        Set iConf = CreateObject("CDO.Configuration")
        Set Flds = iConf.Fields
        ‘* Assign values to Flds class properties
        With Flds
            .Item("
http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
            .Item("
http://schemas.microsoft.com/cdo/configuration/smtpserver") = sSMTPServerName
            .Item("
http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 25 
            .Update
        End With
        ‘* Setting up the Body of the Email message
        sBody = ServerName & " is not pingable on " & Now
        ‘* Assign message properties and Send the mail
        With iMsg
           Set .Configuration = iConf
           .Fields("urn:schemas:httpmail:importance").Value = 2    ‘Setting Mail importance to High (2)
           .Fields.Update
           .To = MailTo
           .From = MailFrom           ‘Fake, but make-sense email FROM address
           .Sender = MailFrom               ‘Return Email address
           .Subject = ServerName & ": Not Pingable"
           .TextBody = sBody
           .Send
        End With
End Sub

One thought on “VBscript: Pinger or Ping servers and alert Sys Admins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s