IP-Updater / DynDNS script via Perl
For years and years, I've been using the dynamic DNS service at DynDNS.net to make my home machine accessible on the web via a static hostname. DynDNS even supplies you with a script to update DNS records on-the-fly without some nasty third party programs being required. This especially made sense for me because I am using the DynDNS updater on my Linux server, thus no windows taskbar stuff would have worked anyway.
This Perl script realizes a simple solution to make DynDNS on-the-fly updates possible. It also features a logging mechanism which prints all IP changes to a file.
This is a part of my log file which is running since 2004:
2006-01-06, 10:36:01 => IP changed to 217.225.225.69
2006-01-07, 05:39:01 => IP changed to 217.225.227.96
2006-01-07, 12:01:01 => IP changed to 217.81.240.219
2006-01-07, 14:32:01 => IP changed to 62.226.70.222
2006-01-07, 16:31:01 => IP changed to 217.225.225.59
2006-01-08, 05:39:01 => IP changed to 217.225.233.78
2006-01-08, 06:55:01 => IP changed to 62.226.65.25
2006-01-09, 05:39:01 => IP changed to 217.225.226.211
2006-01-09, 07:22:01 => IP changed to 62.226.77.73
2006-01-09, 22:58:01 => IP changed to 84.135.204.180
2006-01-10, 03:59:01 => IP changed to 62.226.71.54
The actual updates are done via my updatedns.pl:
#!/usr/bin/perl
use LWP::Simple;
my $page = "http://www.whatismyip.com";
my $dnspage = http://www.dynup.net/update/java.php3?hostname=myhostname&password=mypassword;
my $ipfilepath ="/srv/www/htdocs/ipdaemon/ip.txt";
my $updatefilepath ="/srv/www/htdocs/ipdaemon/ipupdates.txt";
$begincut = "<TITLE>WhatIsMyIP.com - ";
$endcut = "</TITLE>";
$_ = get($page);
s/^.*$begincut//is;
s/$endcut.*$//is;
open( FILE, "<$ipfilepath" );
my $oldip = <FILE>;
close FILE;
if ($_ ne $oldip)
{
my $update = get($dnspage);
if($update =~ m/complete/i)
{
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$mon++;
$year+=1900;
if ($mon < 10) { $mon = "0" . $mon; }
if ($mday < 10) { $mday ="0" . $mday; }
if ($sec < 10) { $sec ="0" . $sec; }
if ($min < 10) { $min ="0" . $min; }
if ($hour < 10) { $hour ="0" . $hour; }
open( FILE, ">$ipfilepath" );
print FILE $_;
close FILE;
open( UPDATE, ">>$updatefilepath" );
print UPDATE $year . "-" . $mon . "-" . $mday . ", " . $hour . ":" . $min . ":" . $sec . " => IP changed to " . $_ . "\n";
close UPDATE;
print "update complete";
}
}
What this does is grab my external IP address from a third party website which is rock solid (WhatIsMyIp.com) and print it to my "IP file", if the IP has changed since the last call. The "IP file" just contains my current IP, nothing more. If the IP has changed indeed, I call the DynDNS.net update script which contains my hostname and my password to trigger the DNS update process. In the last step, we update the IP log to reflect the latest change. On my machine, I call the script regularly via a cronjob.
The DynDNS log file makes sense if you want to keep track of your IP changes for various reasons. In my case, I often had problems with a flaky connection until I complained to my ISP and supplied them with the actual timestamps of IP changes to prove the problem to them.