#!/usr/bin/perl # # Check ip address, and update DDNS for "ddo.jp" # # # parameters # "ddo.jp" ID & PASSWD local $ID = 'xxxxxxxxxx'; # Login ID(It serves as a domain name) local $PASSWD = 'xxxxxxxxxx'; # Login password # file names local $CRT_IPF = '/tmp/CRT_IP2.dat'; local $LOG = '/var/log/ddns.log'; # Check current ip address on the appointed URL web page. local $CHK_URL="http://info.ddo.jp/remote_addr.php"; # local $INTERVAL = 1209600; # 2 weeks # $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"; #--------------------------------------------------- # main my ($NEW_IP,$CRT_IP,$CRT_TIME,$i); # get current ip address which set as the domain. $CRT_IP = ""; $CRT_TIME = "0"; open(INPUT, $CRT_IPF); foreach(<INPUT>){ chop; /^IP:/ && do{ $CRT_IP = $' }; /^TIME:/ && do{ $CRT_TIME = $' }; } close(INPUT); # check a assigned ip address open(INPUT, "lynx -dump \"$CHK_URL\" | "); foreach(<INPUT>){ /([0-9\.].*)/ && do{ $NEW_IP = $1}; } close(INPUT); # Lapsed time from the last update $i = time() - $CRT_TIME; # change DDNS, supposing the IP address is changed. if ( ( ($NEW_IP ne "" )&&($CRT_IP ne $NEW_IP) ) || ( $i > $INTERVAL) ) { # change DDNS open(INPUT2, "lynx -dump \"http://ddo.jp/dnsupdate.php?dn=$ID&ip=$NEW_IP&pw=$PASSWD\" |"); # check whether change of DDNS has been successful foreach(<INPUT2>){ /SUCCESS: / && do{ $TEMP = 1;}; } if( $TEMP == 1){ # save a new IP address. $i = time(); open (OUTPUT ,">$CRT_IPF"); print OUTPUT "IP:$NEW_IP\nTIME:$i\n"; close OUTPUT; # write a message on the log file $time = conv_date(time()); open(LOG, ">> $LOG"); print(LOG $time . ":change \"" . $ID . ".ddo.jp\" <= " . $NEW_IP . "\n"); close(LOG); } } sub conv_date{ my ($times,$mode) = @_; my ($sec,$min,$hour,$mday,$month,$year,$wday); ($sec,$min,$hour,$mday,$month,$year,$wday,undef,undef) = localtime($times); $month++; $year += 1900; $times = sprintf("%d/%02d/%02d %02d:%02d", $year, $month, $mday, $hour, $min); return($times); } |