#!/usr/bin/perl -w
# ^^^^^^^^^^^^^ set this to your path to perl


# Script name: ipupdate.pl
# Author: Werner Bleckwendt
# email: we_bl@yahoo.de
# Date: 6/6/2002
# Purpose: Updates the IP number when a new connection gets established
# has been tested with freedns.afraid.org and worked fine for me.
# As always .... there is no warranty of any kind, however you may use
# and modify this script to fit to your need.
########################################################################



my (%url, $notify, $always, $mailprogram, $local_file, $TimeStamp, $text, $domain, $rc, $url);



use LWP::Simple;



# Configure the URL's to reflect your update string(s)
# entries must be in the form "descriptive name","complete URL"
# You can use as many as you like.


%url=("domain 1", "http://freedns.afraid.org/dynamic/update.php?UC1YZHNOamExZllBQVgtR0xMYzoyNzI1NzM=",
                "domain 2","http://freedns.afraid.org/dynamic/update.php?UC1YZHNOamExZllBQVgtR0xMYzoyNzI1NzA=");


# Put your email address in this variable to get an email, if an error accours


$notify='freedns@pmud.com';
# If you want to be notified everytime an update was executed, set this to "yes"
$always="yes";


# sendmail 
$mailprogram="/usr/sbin/sendmail";


# put this file in /usr/local/bin and make it excecutable (chmod 755)
# add the following to the end of your ip-up script (on a SuSE machine it is
# usually  /etc/ppp/ip-up):
# /usr/bin/perl /usr/local/bin/ipupdate.pl 2>&1 &


# done


###############################################################################
#
#       You should not need to modify anything below
#
###############################################################################


# This variable is used for the download file
$local_file="/tmp/freedns.tmp";
$TimeStamp = localtime(time);
$text="";


# wait (seconds) to make sure a connection has been established
sleep 10;


# Don't use the buffer
$|=1;


        while (($domain,$url) = each ( %url)) {
        
          $rc = mirror("$url","$local_file");
                
                print "ipupdate.pl executed. Domain: $domain Return code $rc\n";          
          
          if (is_success($rc)){
                open (TX, $local_file) || die "Can't open Return File $!";
                $text =<TX>;
                close TX || die "Can't close Return File $!";
                if ($text =~ /.*fail.*/ig){ $rc="490"};
            }


          if (is_error($rc) || $always =~ /yes/i){
                        &mail_error;
               }


        unlink $local_file;
        }


sub mail_error {
        open (MAIL, "|$mailprogram -t") || die "Can't open $mailprogram!\n$!\n";
        print MAIL <<EOT;
To: $notify
From: ipupdate\@localhost
Subject: IP Update


While attempting to update the IP number for 
        $domain
with your DDNS, an error/success code was 
returned: $rc


        $TimeStamp


        The URL was
$url



EOT


if ($text ne "") {
print MAIL <<EOT
The message returned by the host was:
$text


EOT


}


        print MAIL "Your ip-update script\n";



        close (MAIL) || die "Can't close $mailprogram!\n$!\n";


}
### end of script ###
