From: Kevin O. <ke...@ol...> - 2002-10-03 15:35:12
|
I got this from the net somewhere long ago (maybe freshmeat), it just emails my work email and cell phone when the stored address is different from the found address. I run it from cron every five minutes. I'd imagine this problem has been solved in bunches of different ways. Kevin #!/usr/bin/perl -w $oldIPFilename = "/root/dhinfo/oldIP"; # dir to store state info $email = "your\@email.com"; # address to notify changes $email = $email . " phonenumber\@mobile.att.net"; #$subject = "IP Change"; $link = "eth0"; # name of link to watch sub GetIP { my $currentIP; open(IFCONFIG,"/sbin/ifconfig $link|"); while (<IFCONFIG>) { if (/inet addr:/) { my @data = split; $currentIP = $data[1]; $currentIP =~ s/addr://; } } close(IFCONFIG); return($currentIP); } sub GetOldIP { open(OLDIP,$oldIPFilename); my $oldIP = <OLDIP>; close(OLDIP); return($oldIP); } sub MailChanges { $currentIP = shift; my $msg = "From: root\@mh.machine.com\n"; $msg = sprintf("%sSubject: IP change\n\n", $msg); $msg = sprintf("%sIP change for mh.machine.com. New IP is \n\t%s", $msg, $currentIP); print $msg; my $command = sprintf("echo \"%s\" | /usr/sbin/sendmail -f dhcp\@mh.machine.com %s", $msg, $email); system($command); } sub WriteNewIP { my $currentIP = shift; open(FILE,">" . $oldIPFilename); print FILE $currentIP; close(FILE); } ################ my $currentIP = GetIP(); my $oldIP = GetOldIP(); if ($currentIP ne $oldIP) { MailChanges($currentIP); WriteNewIP($currentIP); } |