Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

FreeDNS_Updater

by TZapper (Acolyte)
on Aug 12, 2004 at 07:52 UTC ( #382183=sourcecode: print w/replies, xml ) Need Help??
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__
Replies are listed 'Best First'.
Re: FreeDNS_Updater
by naChoZ (Curate) on Aug 12, 2004 at 12:43 UTC

    Just a couple comments. When writing scripts specifically for use as background processes, it is good practice to make sure they respond to signals. Have a look at one of mine for example at Window Maker background/theme rotator.

    Also, an indent of four spaces per stop is standard practice for many.

    --
    "A long habit of not thinking a thing wrong, gives it a superficial appearance of being right." -- Thomas Paine
    naChoZ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://382183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-12-15 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?