Category: | Web Stuff |
Author/Contact Info | Aleksandr Melentiev |
Description: | A script to automatically update dynamic DNS with http://freedns.afraid.org service. Version updated to 1.2. Changes include: + Signal handling (per Nachoz's recommendations and code sample) + self daemonazation |
#!/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__
|
Back to
Code Catacombs