Script for IP Change Alert

If you need to be notified upon the IP address of the system it can be achieved through following Perl Script

At first a sendemail needs to be installed using :
sudo apt-get install sendemail (in Debian based Distros : Ubuntu )
sudo yum install sendemail (in Redhat Based Distros :Red Hat,Fedora, Cent OS)

Else you may use another MTAs too like sendmail or postfix that is available.

ThisĀ  IP change alert script alerts the admin if external IP address is changed

#!/usr/bin/perl -w
#Anwesh Tiwari Oct 12 2011

use strict;
my $today=`date +%Y-%m-%d” “%r`;
#location of TEMP file
my $ip_last_check_file = “/home/tmp/IP-Address”;
my $ip_now = `curl -s http://checkip.dyndns.org`;
$ip_now =~ s/.*?(d+.d+.d+.d+).*/$1/s;

open(IP, “<$ip_last_check_file”) or die “Cannot open $ip_last_check_file: $!”;
my $ip_last_check = ;0
close(IP);

if ($ip_now ne $ip_last_check)
{
open(IP, “>$ip_last_check_file”) or die “Cannot open $ip_last_check: $!”;
print IP $ip_now;
close(IP);
#-s “SMTP address of your SMTP server instead” of “-s localhost” if you dont have ipaddress running.
my $output = `sendemail -f [email protected] -t [email protected] -s localhost -u IP-CHANGE-ALERT -m Gateway IP-address has changed to $ip_now.\\n Event Time :$today`;
}

exit;

Make sure that port 25 is not blocked by ISP

email

Leave a Reply