#!/usr/bin/perl -w
#
# Kotnet DNS updater
# Goldie 2009
#
# Needs     WWW::Mechanize (libwww-mechanize-perl)
#       Crypt::SSLeay (libcrypt-ssleay-perl)
#
# Windows: use ppm to install packages and change getIp()

use strict;
use WWW::Mechanize;

### kullogin data, host to modify (without .kotnet.org) and iface to find ip
my $username = "";
my $password = "";
my $host = "";  
my $interface = "eth0";
###
    
my $updatescript = "https://dns.kotnet.org/secure/host.php?hostname=$host";

my $mech = WWW::Mechanize->new(agent => "Auto DNS updater 0.1");
my $ip = getIp();

sanityCheck();

print "Shibboleth...\n";
$mech->get($updatescript) or die;
$mech->submit() or die;

print "Central kullogin...\n";
$mech->set_visible( $username, $password ) or die;
$mech->submit or die;

print "Update ip to $ip...\n";
$mech->field("ip" => $ip) or die;
$mech->click_button(name => "update");

if ($mech->content =~ m/$ip/) {
    print "Done...\n";
    exit 0;
} else {
    print "Failed...\n";
    exit 1;
}

sub getIp
{
    `ifconfig $interface | grep "inet addr"` =~ m/:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})  Bcast/;
    return $1;
}

sub sanityCheck
{
    if ($username eq "" || $password eq "" || $host eq "") {
        die "!! You need to enter your username, password and hostname !!";
    }
}
