Strangely enough, I never had any need for a dynamic DNS service until this week. In retrospect, it really does seem odd that I've never needed such a service before now, but so be it.
My problem this week was that I wanted to set up a http://www.dyndns.org, sign up for a free account, and it's done. I assigned my current cable dynamic ip address to the hostname I chose, and now "xyz.dyndns.info" points to my cable modem.
In my case, I could have stopped there. The only time the dynamic ip changes is when I change routers. While that is something I do fairly frequently in the course of testing things, I certainly didn't need to do it during the run of this demo, and even if I did, I could pop back to dyndns.org and update myself.
But what if I needed this for a longer term or if my dynamic ip address were more dynamic than it is now? Well, there are hundreds and hundreds of clients that will update dynamic dns services. Many home-use routers even have a client built into them (though the services warn against using these because most update too often for no reason).
Well, as usual, I don't want to use someone else's code. I'd rather understand how the thing works and do it myself. I might use someone else's code after that, but almost always I want to get my own hands dirty first.
So, first problem: how to find the current ip address? If you are behind a firewall, there may be some way to get the device or computer to tell you. It might be as simple as an "ifconfig eth1" if you are using a Linux router. But I change firewalls frequently, so I can't depend on that.
There are places on the net that can help. For example, you can use lynx --dump http://yourserver.com/whatsmyip.html?564312 > /dev/null
The "whatsmyip.html" doesn't exist, and the extra "?563412" is just a number you make up. These accesses will be logged, so on the server we'll find these in our access_log:
Found an error or have a suggestion? Let us know and we'll review it.
66.31.38.153 - - [18/Sep/2004:10:01:01 +0000] "GET /whatsmyip.html?564312 HTTP/1
.0" 404 2240 "-" "Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7"
66.31.38.153 - - [18/Sep/2004:11:01:01 +0000] "GET /whatsmyip.html?564312 HTTP/1
.0" 404 2240 "-" "Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7"
66.31.38.153 - - [18/Sep/2004:12:01:01 +0000] "GET /whatsmyip.html?564312 HTTP/1
.0" 404 2240 "-" "Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7"
66.31.38.153 - - [18/Sep/2004:13:01:16 +0000] "GET /whatsmyip.html?564312 HTTP/1
.0" 404 2240 "-" "Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7"
66.31.38.153 - - [18/Sep/2004:14:01:01 +0000] "GET /whatsmyip.html?564312 HTTP/1
.0" 404 2240 "-" "Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7"
A simple shell script can extract "66.31.38.153" which is what we need to know. That script could continue on to update the dynamic dns service.
So how do you update the service? Every service has their own method, but we'll just look at dyndns.org. You can find the full details at #!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
$debug=0;
$checkip=1;
chdir("/root/dyndns") or die "can't chdir /root/dyndns $!";
# Try to get a lock just in case we got called again
# this isn't 100% foolproof but is good enough for this purpose
open("O",">lock");
flock(O, 2) ; #block here
if (-e "badstatus") {
  logit("badstatus
");
  myexit(0);
}
# Don't check more often than every ten minutes.
# We call this with cron, but this just makes sure
open(F,"currenttime");
$last=<F>;
chomp $last;
close F;
$last+=600;
$now=time();
if ($checkip) {
  while ($last > $now) {
  
printf "sleeping %0.2d
",($last - $now);
  
sleep ($last - $now);
  
$now=time();
  }
  open(F,">currenttime");
  print F $now;
  close F;
  $what="http://checkip.dyndns.org/";
  $ua=LWP::UserAgent->new();
  $ua->agent("APL Perl/1.0");
  $req=HTTP::Request->new(GET=>$what);
  $response=$ua->request($req);
  if ($response->is_error() ) {
  
logit("Couldn't get $what");
  
myexit(0);
  } else {
  
$currentip=$response->content();
  
$currentip=~ s/.*Current IP Address: *//;
  
$currentip=~ s/..body.*//;
  
chomp $currentip;
  
open(F,"currentip");
  
$lastip=<F>;close F;
  
chomp $lastip;
  
open(F,">currentip");
  
print F "$currentip
";
  
close F;
  }
}
open(F,"lastupdate");
$lastup=<F>;close F;
chomp $lastup;
$days=($now - $lastup)/(3600 * 24);
if ($days >= 28 or $lastip != $currentip or $debug) {
  
logit("update $days $lastip $currentip
");
   
$what="http://members.dyndns.org/nic/update?system=dyndns&hostname=test.dynd
ns.org&myip=$currentip";
  
$ua=LWP::UserAgent->new();
  
$ua->agent("APL Perl/1.0");
  
$req=HTTP::Request->new(GET=>$what);
  
$req->authorization_basic('test', 'test');
  
$response=$ua->request($req);
# Lynx equivalent is
#
# lynx --dump -auth youraccount:yourpass "http://members.dyndns.org/nic/update?system=dyndns&hostname=yourhost.dyndns.info&myip=$currentip"
#
  
if ($response->is_error() ) {
   
print "Error
";
   
myexit(0);
  
} else {
   
$status=$response->content();
   
print "$status
";
   
open(F,">lastupdate");
   
print F $now;close F;
   
myexit(0) if ($status =~ /good/ or $status =~ /nochg/);
   
logit("Dyndns says $status
");
   
myexit(0);
  
}
}
sub myexit {
  
$a=shift;
  
flock(O, 8);
  
close O;
  
exit $a;
}
sub logit {
  
$a=shift;
  
print $a;
}
*Originally published at
Suggest a Correction
Dynamic DNS Services Update Scripts
0 views
Comments (0)
Please sign in to leave a comment.





No comments yet. Be the first to comment!