#!/usr/bin/perl
my $server='[server]';
my $url='/wilddns/wilddns.php';
my $user='[user]';
my $pass='[pass]';
my $host='[host]';
my $addr='*';

my $if='[interface]';
my $lastipf='/var/log/wilddns.lastip';
my $out='/var/log/wilddns.out';

## no need to change anything below this line
## -----------------------------------------------------------------------------------------------------------
use strict;
use LWP::UserAgent;
use Data::Dumper;

## file handling subroutines
sub wprint { open (OUT,">$_[0]"); print OUT $_[1]; close (OUT); }
sub wread { open (IN,"<$_[0]"); my $in=<IN>; close (IN); return $in; }

## load last updated ip
my $lastip=wread($lastipf);

## get current ext ip
my @ips=(`ifconfig $if`=~/inet addr:(\S+)/g);
my $ip=@ips[0];

## check for ip change
if ($ip eq $lastip) {
  #print "Still the same :)\n";
  exit(0);
}

## prepare uri
my $uri="http://${user}:${pass}\@${server}${url}?addr=${addr}&host=${host}";

## access wilddns
my $ua=LWP::UserAgent->new();
my $re=$ua->request(HTTP::Request->new(GET=>$uri));
if ($re->is_success){
  wprint($lastipf,$ip);
}
else { #'WWW-Authenticate' is the realm-name <- ??
  die "Error: ",$re->header('WWW-Authenticate') || 'Error accessing',
      "; ", $re->status_line, "\n at $url\n Aborting";
}

## output response to out file
#print Dumper($re);
wprint($out,$re->{_content});

exit(0);
