Blog Archive

Wednesday, October 19, 2011

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 sendmail (in Debian based Distros)
Else you may use another MTAs too like sendmail or postfix that is available.
#!/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 = <IP>;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 sentname\@yourdomain.com -t alertaddress\@yourdomain.com -s localhost -u IP-CHANGE-ALERT -m Gateway IP-address has changed to $ip_now.\\\\n Event Time :$today`;
}

exit;


0 comments:

Post a Comment

Followers