Portræt af Arne Jørgensen
Arne Jørgensen
[Download]
#!/usr/bin/perl -w

# Copyright (C) 2005, 2006 Arne Jørgensen

# Author: Arne Jørgensen <arne@arnested.dk>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.

### Commentary:

# I use this script to update my dynamic ip address at GratisDNS.

### Code:

use strict;

use Net::DNS;
use Net::HTTPS;
use Net::Netrc;

# domanins at GratisDNS to update
my @domains = ('arnested.dk',
               'rrc-wurfel.dk',
               'xn--rrc-wrfel-u9a.dk',  # rrc-würfel.dk
               'xn--jssen-vua.dk',      # jøssen.dk
               'joessen.dk',
               'ctan.dk');

my $resolver = Net::DNS::Resolver->new;
my $ip = get_ip('arnested.dyndns.org');

if ( not defined $ip ) {
    die "Could not resolve my own IP number.\n";
}

my $server = Net::HTTPS->new(Host => "ssl.gratisdns.dk") || die $@;

my $mach = Net::Netrc->lookup('ssl.gratisdns.dk'); # read ~/.netrc
my $username = $mach->login;      # username at gratisdns.dk
my $password = $mach->password;   # ddns password at gratisdns.dk

$resolver->nameservers("ns1.gratisdns.dk", "ns2.gratisdns.dk");

foreach my $domain (@domains) {
    update_ip($domain);
}


sub get_ip {
    my $answer = $resolver->query($_[0], 'A');

    if ($answer) {
        foreach my $rr (grep {$_->{'type'} eq "A"} $answer->answer) {
            return $rr->{'address'};
        }
    }
}

sub update_ip {
    my $host = $_[0];
    my @domain = split /\./, $host;
    my $domain = $domain[$#domain-1] . "." . $domain[$#domain];
    my $hip = get_ip($host);
    if ( defined $hip) {
        if ( $ip ne $hip ) {
            $server->write_request(GET => "/ddns.phtml?u=$username&p=$password&d=$host&h=$domain");
        }
    } else {
        warn "Could not resolve IP number for $domain.\n";
    }
}

# update-dns ends here