#!/usr/bin/perl
#
$OLD_IPF = 'OLD_IP.dat';
$NEW_IPF = 'NEW_IP.dat';
$WGET_RESULT = 'wget.log';
$LOG = '/var/log/ddns.log';

open INPUT,"$OLD_IPF";
$OLD_IP=<INPUT>;
chomp($OLD_IP);
$OLD_IP =~ s/^\s*(.*?)\s*$/$1/;
close INPUT;

system("wget -q -O $NEW_IPF http://dyn.value-domain.com/cgi-bin/dyn.fcg?ip");
open(INPUT,$NEW_IPF);
$NEW_IP = <INPUT>;
chomp($NEW_IP);
$NEW_IP =~ s/^\s*(.*?)\s*$/$1/;
close(INPUT);

if($NEW_IP ne "" && $OLD_IP ne $NEW_IP){
	system("wget -q -O $WGET_RESULT 'http://dyn.value-domain.com/cgi-bin/dyn.fcg?d=<span style="color:red">ドメイン</span>&p=<span style="color:red">パスワード</span>&h=*'"); 

	open WLOG,"$WGET_RESULT";
	$wgr=<WLOG>;
	chomp($wgr);
	$wgr =~ s/^\s*(.*?)\s*$/$1/;
	close WLOG;

	if($wgr eq "status=0"){
		open(OUTPUT, ">$OLD_IPF");
       		print OUTPUT $NEW_IP;
        	close OUTPUT;
	}

        $now = localtime;
        open (OUTPUT ,">>$LOG");
        print OUTPUT "$now DDNS IP Address Updated. $OLD_IP to $NEW_IP $wgr\n";
        close OUTPUT;
}
