Tuesday, June 10, 2014

PowerShell Script : WAN Monitoring Script with Email Alert

 

Wan Monitoring

 

If you are using two WAN connections in your office or in organization, and if you are not monitoring them, it’s a considerable problem.

In my office, I have two internet connections. One is configured as primary connection and the other one is configured as backup internet line.

Once a not so long time ago, on a very  bad day my primary WAN gets down and the firewall router switched to the backup internet line.

I was not monitoring my Internet Line. {bad, very bad habit }.

I was not ware of this.

Internet was working but little bit slow. I was not looking in to it first because with ISP these kind of glitches does happens sometimes.

But in the evening, when things get too slow, I check the static ip of my internet connection and I came to know, router is switched to backup line.

ARRRRRRRRRRRR !!! Irritating.

To solve this issue for me, I wrote a simple PowerShell script.

I scheduled this script to run after every one hour.  It's check the  Static IP  of the current internet line, and if static IP doesn't match with Primary WAN IP, it sent an email alert.

What this script does?

1.    You have to set your primary WAN IP in to the script

2.    Script check your current Static IP

3.    If the Static IP doesn’t match with the WAN1,

4.    It send you an email.

 

Simple and elegant script.

 

You can download the script from TechNet galleryhttp://gallery.technet.microsoft.com/WAN-using-PowerShell-and-bc1bd828 

 

Here is code for the script, and all code is easy to understand.

  

#==================| Satnaam Waheguru Ji |===============================   
#
# Author : Aman Dhally
# E-Mail : amandhally@gmail.com
# website : www.amandhally.net
# twitter : @AmanDhally
# blog : http://newdelhipowershellusergroup.blogspot.in/
# facebook: http://www.facebook.com/groups/254997707860848/
# Linkedin: http://www.linkedin.com/profile/view?id=23651495
#
# Creation Date : 9- June - 2014
# File :
# Purpose : Check the static ip and compare it with WAN 1
# Version : 5
#
#
# My Pet Spider : /^(o.o)^\
#
#========================================================================

#Note :- This script function is PowerShell V2 compatible.
# We can add Advnace Function to it, But let's keep this things simple :) .


function Check-WanStatus {


begin {
try {

# SMTP Variables
$smtp = 'YOUR_SMTP_SERVER'
$to = 'amandhally@gmail.com'
$from = 'POSH@YourDomain.com'
$subject = 'WAN-1 is Down'



# Wan Variables

#WAN1 - Primary IP Address
$wan1 = '182.73.114.40'
#WAN2 - IP Address
$wan2 = '122.154.3.97'
#Getting Static IP from URL
$url = "http://checkip.dyndns.com"
$webclient = New-Object System.Net.WebClient
}
catch {
}
}
process {
try {

$Ip = $webclient.DownloadString($url)
$Ip2 = $Ip.ToString()
$staticIP = $Ip2.Split(" ")[5].Replace("</body>","").Replace("</html>","")

if ( $staticIP -notmatch $wan1 )
{
Write-Output "WAN-1 {$wan1} is Down. The Current Static IP is : $wan2 ."

$body = "WAN-1 {$wan1} is Down."
$body += "`n"
$body += "`n"
$body += "The Current Static IP is : $staticIP"

Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -Priority 'High' -UseSsl


}

else
{
Write-Output "All is Well"
}
}
catch {
}
}
end {
try {
}
catch {
}
}
}

Check-WanStatus


Below is the screenshot of the warning email.

 

09-06-2014 12-40-00

You can download the script from TechNet galleryhttp://gallery.technet.microsoft.com/WAN-using-PowerShell-and-bc1bd828 


Regards


Aman Dhally.


 



Come and join my journey of : “100 Days of Self Improvement” on


 


Facebook: https://www.facebook.com/100DoSI 


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.