Looping Host Ping Test

Posted: June 29, 2012 in Information Technology, Networking, Scripts

Pretty simple script here. Just needed something to loop every 10 minutes and run a number of pings against some hosts, then email me only if there were any drops.

$emailFrom = “ping@mydomain.com”
$emailTo = “me@mydomain.com”
$smtpServer = “smtp.mydomain.com”

Function LostPingCount ()
{
param ( [string]$TargetHost = “” )
$PingResult = [string](ping -w 1 -n 15 $TargetHost | Select-String “\(“)
$Start = $PingResult | Select-String “\(” | %{ $_.Matches[0] }
$End = $PingResult | Select-String “\%”| %{ $_.Matches[0] }
$LostPings = $PingResult.Substring(($Start.Index + 1), ($End.Index – $Start.Index – 1))
Return $LostPings
}

While ( 1 -eq 1 ) {
ForEach($PingTarget in Get-Content C:\Scripts\PingTest.txt){
$NumLostPings = LostPingCount $PingTarget
If ( $NumLostPings -ne 0 ){
$subject = “$PingTarget Failed Ping Test”
$body = “The failed ping count for host $PingTarget was $NumLostPings out of 15”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
}

Start-Sleep -Seconds 600

}

Advertisements

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 )

Google+ photo

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

Connecting to %s