Register
It is currently Thu Sep 18, 2014 1:40 am

Connectivity Monitor


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Mar 01, 2011 8:29 am   

Joined: Tue Mar 01, 2011 8:05 am
Posts: 3
So heres the thing... my scripting knowledge is limited at best, and I have always been a windows guy... I found a powershell script that met my basic needs:

E-Mail immediately when a ping is returned notifying that my WebSense website blocking appliance wasnt functioning properly.

E-Mail immediately when functionality has been restored...

Powershell works great but instead of running this on my machine 24 hours it has been requested that I convert to shell script so we can run this off of a linux box... so now I am totally lost since I know absolutely nothing about shell scripting, I do have a book that I am reading on it now but it will likely take me FOREVER to figure it all out...
Side Question: Is there any type of conversion setup that can convert powershell to a unix script?? I assume not but figured I would ask...

Here is the powershell script I have now, it reads a file with an IP address of a blocked website ie. gmail.com, the file also has a true/false statement associated with the IP that was written based on the last ping attempt, if the status changes from what is in the file an e-mail is automatically generated.

# -----------------------------------------------------------------------------
# Script: Invoke-PingStats.ps1
# Author: Chris Brown http://www.flamingkeys.com
# Date: 20/12/2010 10:51:51
# Keywords:
# comments:
#
# Version History
#-+-----+-----------+------+----------------------------------------------------+
# | Ver | Date | Name | Changes |
#-+-----+-----------+------+----------------------------------------------------+
# | 1.0 | 20/12/10 | CJB | Initial Script |
# | 1.1 | 17/01/10 | CJB | Updates for release |
#-+-----+-----------+------+----------------------------------------------------+
#
# TODO: - Allow for 3 pings to be missed before alerting
#
# ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠
# Variables
#
# $configFile stores the location of the config xml file
$configFile = "c:\scripts\hosts.xml"
#
# Email Settings
$mailFrom = "Ihatethattheyblockgmail@organization.com"
$mailTo = "workisnofun@organization.com","Iwouldratherbeonmymotorcycle@organization.com"
$mailSubjectPrefix = "[PingStats] "
$mailServer = "Mail Server Here"
#
# ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠ ♥ ♦ ♣ ♠

function Test-Ping ([string]$pingtarget) {
Get-WmiObject -Class Win32_PingStatus -Filter "Address='$pingtarget'"
}

function Send-Email ([String]$hostname, [switch]$up) {
#Send email, $hostname is $state
if ($up) {
# Websense is Down, send the email
$mailSubject = $mailSubjectPrefix + "WebSense has gone DOWN [$(Get-Date -format "HH:mm")]"
$mailBody = @"
<span style="font-family: sans-serif;color: red"><strong>$($hostname)</strong> is now DOWN!</span>
"@
Send-MailMessage -From $mailFrom -SmtpServer $mailServer -Subject $mailSubject -To $mailTo -Body $mailbody -BodyAsHtml
} else {
# Websense is UP, send the email
$mailSubject = $mailSubjectPrefix + "WebSense is back UP [$(Get-Date -format "HH:mm")]"
$mailBody = @"
<span style="font-family: sans-serif;color: green"><strong>WebSense</strong> is back UP!</span>
"@
Send-MailMessage -From $mailFrom -SmtpServer $mailServer -Subject $mailSubject -To $mailTo -Body $mailbody -BodyAsHtml
}
}


while (1 -eq 1) {
$results = @()
# Get the latest xml data
[xml]$xml = Get-Content $configFile
# For each host
Clear-Host
foreach ($comp in $xml.hosts.host) {
# Test a ping
$ping = Test-Ping $comp.ip
if ($ping.StatusCode -ne 0) {
##The host is offline
if ($comp.lastresult -eq $true) {
# if it was up last ping, send an email and rewrite the xml
Send-Email -hostname $comp.InnerXml
$comp.lastresult = "false"
} else {
# if it was down last ping, do nothing
}
Write-Host $comp.InnerXml "`t("$comp.ip")`t x ms" -ForegroundColor White -BackgroundColor Red

} else {
##The host is online
if ($comp.lastresult -eq $true) {
# if it was up last ping, do nothing
} else {
# if it was down last ping, send an email and rewrite the xml
Send-Email -hostname $comp.InnerXml -up
$comp.lastresult = "true"
}
# Write to screen
Write-Host $comp.InnerXml "`t("$comp.ip")`t" $ping.ResponseTime "ms" -ForegroundColor black -BackgroundColor Green
}


}
# Save the updated xml out
$xml.Save("c:\scripts\hosts.xml")
$results
Start-Sleep -Seconds 5
}


Top
 Profile  
 PostPosted: Tue Mar 01, 2011 8:31 am   

Joined: Tue Mar 01, 2011 8:05 am
Posts: 3
- <hosts>
<host ip="64.233.161.83" lastresult="false" site="">Gmail</host>
</hosts>

Thats what my host XML file looks like if it would be helpful...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP