Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello all! 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 use Net::SMTP::SSL; # FETCH THE PUBLIC IP ADDRESS $ip = `wget http://automation.whatismyip.com/n09230945.asp -O - -q ; e +cho`; chomp ($ip); # 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>; chomp ($currentIP); close 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; close FILE; &emailAdmin; } # E-MAIL SUB ROUTINE # (I HAVE EXIM4 SETUP TO FORWARD TO GOOGLE # SMARTHOST SO I CAN RECEIVE MAIL AS ROOT) sub emailAdmin { 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\""); }

In reply to Script to Notify of Public IP Change by Arkevius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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:
    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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2014-12-25 09:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (159 votes), past polls