#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Script to Notify of Public IP Change


    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?

    Code:
    #! /usr/bin/perl -w
    
    use Net::SMTP::SSL;
    
    # FETCH THE PUBLIC IP ADDRESS
    $ip = `wget http://automation.whatismyip.com/n09230945.asp -O - -q ; echo`;
    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: Heirloom mailx 12.5 6/20/10\nMIME-Version: 1.0\nContent-Type: text/plain; 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\"");
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    They have cable internet, and unsure of whether or not their ISP provided them with a static or dynamic IP address
    The simple and best solution for that problem is to call the ISP and ask them what they are providing.

    Before I comment on your code I must ask, if they are providing a dynamic address, which would be wrong in this case, have you thought of using DDNS and a hostname (URL)?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by FishMonger
    The simple and best solution for that problem is to call the ISP and ask them what they are providing.

    Before I comment on your code I must ask, if they are providing a dynamic address, which would be wrong in this case, have you thought of using DDNS and a hostname (URL)?
    I did take DDNS into consideration, but didn't want to do the extra set-up work (lazy in that way), and didn't want to pay for a third-party service. Also, since I'm still new to Perl, I wanted to challenge myself with coming up with my own solution.

    @spacebar208: Thanks for those links! I'll give them a more thorough reading when I have some time later.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Static or Dynamic IP


    It depends upon the company you get the services of. There are many good and reliable sites which offer static and dynamic IPs at very reasonable pricing such as proxy rental. Their trial is free. I have got their support.

IMN logo majestic logo threadwatch logo seochat tools logo