VBS Host Monitor

Posted on September 4, 2008 by Ben Cecka

Here’s a pretty simple script I put together that I’m using to monitor our VPN tunnel. It’s fairly basic but I’ve extended it so it can be used in a number of ways. It’s been a lifesaver over the past few days so I thought I would share.

set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

' // BEGIN CONFIGURATION OPTIONS //

arrIPAddr = Array("192.168.1.1", "10.0.10.1", "172.16.1.1") 'Array of IPs
strDirectory = "C:\Path\to\logs\" 'Logfile directory
strFile = "HostMonitor.txt" 'Logfile name
strTestLocalConn = "google.com" 'A public server to test your connection

'Email Settings
strEmailTo = "" 'To email address (somebody@domain.com)
strEmailFrom = "" 'From email address (somebodyelse@domain.com)
strEmailSubject = "" 'Email subject
strSMTPGateway = "" 'SMTP server (smtp.emailgateway.net)
strUseSMTPAuth = false 'Change to true to authenticate (true|false)

‘Only if strUseSMTPAuth is true
strSMTPUsername = “” ‘SMTP username
strSMTPPassword = “” ‘SMTP password
strSMTPPort = 25 ‘SMTP port number
strSMTPSSL = false ‘SMTP SSL option

‘ // END CONFIGURATION OPTIONS //

dim strIP
strLogTxt = “”
strSendMsg = false

if Reachable(strTestLocalConn) then
for each strIP in arrIPAddr
if not Reachable(strIP) then
strSendMsg = true
strLogTxt = strLogTxt & ” ” & strIP
end if
next
if strSendMsg then
strLogTxt = “The following addresses were unreachable:” & strLogTxt
else
strLogTxt = “All hosts responded normally.”
end if
else
strLogTxt = “Connection to ” & strTestLocalConn & _
” failed. Host monitor aborted.”
end if

const ForAppending = 8
set objFSO = CreateObject(”Scripting.FileSystemObject”)
if not objFSO.fileexists(strDirectory & strFile) then
objFSO.CreateTextFile(strDirectory & strFile)
end if
set objFile = objFSO.OpenTextFile(strDirectory & strFile, ForAppending)

objFile.Write date() & ” ” & time()
objFile.WriteBlankLines(1)
objFile.Write strLogTxt
objFile.WriteBlankLines(2)
objFile.Close

if strSendMsg then
const cdoSendUsingPickup = 1
const cdoSendUsingPort = 2

const cdoAnonymous = 0
const cdoBasic = 1
const cdoNTLM = 2

set objEmail = CreateObject(”CDO.Message”)

objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strLogTxt & strDate

objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpserver”) = _
strSMTPGateway
if strUseSMTPAuth then
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) _
= cdoBasic
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/sendusername”) _
= strSMTPUsername
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/sendpassword”) _
= strSMTPPassword
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpserverport”) _
= strSMTPPort
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpusessl”) _
= strSMTPSSL
end if
objEmail.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”) _
= 60

objEmail.Configuration.Fields.Update
objEmail.Send
end if

function Reachable(strComputer)
dim objShell, objExec, strCmd, strTemp

strCmd = “ping -n 1 ” & strComputer

set objShell = CreateObject(”WScript.Shell”)
set objExec = objShell.Exec(strCmd)
strTemp = UCase(objExec.StdOut.ReadAll)

if InStr(strTemp, "REPLY FROM") then
Reachable = true
else
Reachable = false
end if
end function

Comments

One Response to “VBS Host Monitor”

  1. Ben Cecka on September 5th, 2008 4:37 pm

    ps - Sorry about the bad formatting on the post. The actual script is tabbed out like it should be, but apparently WP’s code tags aren’t as cool as I thought they would be.

Leave a Reply




  •   Feed
  • Prefer email updates?
  • About Me

    I'm an Information Technology Director and have recently specialized in Information Security for a retail shipping solution provider and franchise organization. I've been involved in software design, web applications, managing help desk operations, and corporate technology deployment strategies.

  • Twitter

    • Write Me

    • ben@cecka.org
    • Find Me

    • Facebook
    • Orkut
    • LinkedIn
    • Last.fm
    • Digg
    • Twitter
    • Pownce
    • Kongregate