#!/usr/bin/perl -w ###################################################################### # afraid.pl for freedns.afraid.org # FreeDNS_Updater/1.2 [Aug 12 2004] # Author: Aleksandr Melentiev # E-mail: tzapper@users.sourceforge.net # Description: # This script runs in the background checking for IP changes, if a # change has been detected, it automatically updates the Dynamic DNS # Tested on FreeBSD 5.1 ###################################################################### require 5.004; use LWP; use POSIX qw(setsid); our $VERSION = 1.2; ########################### # Configuration variables # ########################### ### Update URLs, multiple URLs are accepted @url= qw(http://freedns.afraid.org/dynamic/update.php?string1 http://freedns.afraid.org/dynamic/update.php?string2); ### Interface to grab the current IP from, e.g. fxp0, dc0, tun0 $iface = "tun0"; ### E-mail address for error notifications $email = 'your@email.address'; ##################### # End Configuration # ##################### $SIG{CHLD} = 'IGNORE'; $SIG{QUIT} = 'sigQuit'; $SIG{TERM} = 'sigQuit'; $SIG{ABRT} = 'sigQuit'; $SIG{INT} = 'sigQuit'; &daemonize; $sysCmd = "ifconfig $iface | awk '/inet/{print \$2;}'"; $ip = qx{$sysCmd}; $agent = "FreeDNS_Updater/$VERSION"; $browser = LWP::UserAgent->new(); $browser->agent("$agent"); main(); sub main { update(); while (1) { $currentip = qx{$sysCmd}; if ("$ip" ne "$currentip") { update(); $ip = $currentip; } sleep 10; } } sub update { foreach $updlink (@url) { $updurl = $browser->request(HTTP::Request->new(GET => $updlink)); if ($updurl->is_error) { open MAIL,"|mail $email"; print MAIL "$agent: $0 [Couldnt update $updlink]\n"; close MAIL; return 0; } } } sub daemonize { defined($pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; umask 0; $PID = "/var/run/afraid.pid"; open PID,">$PID" or die ( $! ); print PID $$; close PID; } sub sigQuit { unlink $PID; exit; } 1; __END__