home

Quickies, by Andrea Olivato

text

Update DynDns host with current IP with Perl

Updates any DynDns host with current IP address. Useful if put in crontab and executed every 15mins.

#!/usr/bin/perl

# Load Needed Modules
use strict;
use XML::Simple;
use LWP::UserAgent;

# Get login data
my $username="DynDns_user";
my $password="DynDns_user";
my $hostname="yourhost.example.com";

# Create a new useragent
my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/5.0 (X11; ; Linux i686; it; rv:1.9.1) Gecko/20090701');

# Create and perform a request to get current IP address
my $req = new HTTP::Request GET => "http://ip-address.domaintools.com/myip.xml";
my $content = $ua->request($req)->content();

# Create XML class and parse results
my $xml = new XML::Simple (KeyAttr=>[]);
my $data = $xml->XMLin($content);

# Get my current ip address from xml tree
my $ip = $data->{ip_address};
my $old;

# Open script log and read old ip address
open (MYFILE, '/tmp/ip-ch.log');
$old = ;
close (MYFILE);

# Open script log and write new ip address
open (MYFILE, '>/tmp/ip-ch.log');
print MYFILE $ip;
close (MYFILE);

# If new ip is different from previous one
if ($old != $ip) {

        # Create a new useragent
        my $ua = new LWP::UserAgent;
        $ua->agent('Mozilla/5.0 (X11; ; Linux i686; it; rv:1.9.1) Gecko/20090701');

        # Create and perform a request to update hostname associated ip
        my $req = new HTTP::Request GET => 'http://'.$username.':'.$password.'@members.dyndns.org/nic/update?hostname='.$hostname.'&myip='.$ip.'&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG';
        $ua->request($req)->content();
}
 

2 years ago

July 13, 2009
Comments (View)
blog comments powered by Disqus