I manage a Linux Ubuntu 12.04 LTS server for a local police department. They have cable internet, and unsure of whether or not their ISP provided them with a static or dynamic IP address, I wrote a perl script to notify me if the IP address should ever change (since I manage the server remotely).
I only have 1 problem with this scripts. Sometimes, it seems to fetch a "blank" when trying to resolve the IP. It sees the "blank" address as new, so it will then send me an e-mail saying the address has changed. Then an hour later (when the cronjob goes off every hour on the hour), it will fetch the correct IP addy, and then notify again of the "new change." If I had to guess, it's during the actual resolving of the IP address and fetching that information that it seems to get this null IP.
I've pasted my code below for review. What do you all think?
#! /usr/bin/perl -w
# FETCH THE PUBLIC IP ADDRESS
$ip = `wget http://automation.whatismyip.com/n09230945.asp -O - -q ; e
# OPEN FILE THAT CONTAINS THE IP ADDRESS
# AS OF THE LAST TIME THE SCRIPT WAS RAN
open FILE, "<", "/root/scripts/currentIP" or die $!;
$currentIP = <FILE>;
# COMPARE THE OLD IP AND THE FETCHED IP
# FOR DIFFERENCES
if ($currentIP ne $ip)
#IP HAS CHANGED; EMAIL ADMIN THE NEW IP
#AND THEN CHANGE THE IP RECORD FILE
open FILE, ">", "/root/scripts/currentIP" or die $!;
print FILE $ip;
# E-MAIL SUB ROUTINE
# (I HAVE EXIM4 SETUP TO FORWARD TO GOOGLE
# SMARTHOST SO I CAN RECEIVE MAIL AS ROOT)
my $headers = "PD IP Change\nFrom: ipUpdater Script\nUser-Agent: H
+eirloom mailx 12.5 6/20/10\nMIME-Version: 1.0\nContent-Type: text/pla
+in; charset=us-ascii\nContent-Transfer-Encoding: 7bit\n";
my $to = "root";
my $body = "The public IP address for the BPDServer has changed.\n
+\nNew IP: $ip\nOld IP: $currentIP\n.";
system("echo \"$body\" | mail -s \"$headers\" \"$to\"");
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||