Results 1 to 2 of 2
Adding an Automatic Dynamic DNS Update Daemon to Fedora-9
================================================== =======
I have a number of small websites on my home PC which is running Fedora-9 and being a
bit ...
- 01-23-2009 #1Just Joined!
- Join Date
- Apr 2005
- Location
- Bolton, Lancashire, UK
- Posts
- 3
Adding an Automatic Dynamic DNS Update Daemon to Fedora-9
Adding an Automatic Dynamic DNS Update Daemon to Fedora-9
================================================== =======
I have a number of small websites on my home PC which is running Fedora-9 and being a
bit strapped for cash and therefore preferring not to pay the extra premium for a static
IP address, I decided to use Dynamic DNS for these.
My domain name provider is namecheap.com and I have no experience in using Dynamic DNS
with any other provider, so although these two pieces of code definitely work on
Fedora-9 with domain names provided by Namecheap.com, I have no idea whether these will
work with any other domain name provider.
I have called both of these files the same name, but installed them in different directories,
but only the perl script is on the executable path.
The code for both files are at the end of this writeup.
To use Dynamic DNS on Namecheap.com you need to enable it for your domain(s).
To get it to work properly, you also need to carry out the following commands as root:
chown root:root /usr/sbin/AutoUpdateDynamicDNS /etc/init.d/AutoUpdateDynamicDNS
chmod 750 /usr/sbin/AutoUpdateDynamicDNS /etc/init.d/AutoUpdateDynamicDNS
chkconfig --add AutoUpdateDynamicDNS
chkconfig --level 345 AutoUpdateDynamicDNS on
Then:
To start the daemon:
/etc/init.d/AutoUpdateDynamicDNS start
or
service AutoUpdateDynamicDNS start
To check the daemon's status:
/etc/init.d/AutoUpdateDynamicDNS status
or
service AutoUpdateDynamicDNS status
To stop the daemon:
/etc/init.d/AutoUpdateDynamicDNS stop
or
service AutoUpdateDynamicDNS stop
To restart the daemon:
/etc/init.d/AutoUpdateDynamicDNS restart
or
service AutoUpdateDynamicDNS restart
NB. There is a bug which I have not as yet managed to get around in that
each you execute a start a new instance of the daemon is started.
I would be most grateful if someone would send me a fix for this.
Jim Rey
jimrey1943@googlemail.com
--------------------------------------------------------------------------------------
Filename: /usr/sbin/AutoUpdateDynamicDNS
========================================
#!/usr/bin/perl
# -----------------------------------------------
# Dynamic IP Address updating using Namecheap.com
# -----------------------------------------------
#
# When you enable Dynamic DNS for your domain on Namecheap.com,
# it returns information similar to:
#
# Domain Name johnsmith.com
# Host Name anyhost_you_specify_in_client
# Password AEF296D5-F8BD-2E18-AED2-8BC237D53B26
#
# You use the following URL format (url information) to update the IP of your domain:
# http://dynamicdns.park-your-domain.c...%5B&ip=your_ip
#
# For example, in myip.johnsmith.com, myip is the host_name, johnsmith.com is the domain,
# and password will be provided in the dynamic dns page as shown above.
# IP is optional, if you don't specify any IP, the IP from which you are accessing the URL
# will be set for the domain.
# -----------------------------------------------
use LWP::Simple;
use POSIX qw(setsid);
use strict;
# daemonize the program
&daemonize;
my $TESTING = 0; # Set to 1 during testing to print diagnostics
# and limit number of tries.
my ($old_ip, $new_ip, $loopcnt) = ('', '', 10);
# You will need to modify this hash table to suit your own domain(s),
# subdomain(s) and passwords.
my %domains = (
'johnsmith.com' => {
'password' => 'AEF296D5-F8BD-2E18-AED2-8BC237D53B26',
'subdomains' => ['@', 'www', 'anotherip', 'test', 'trial'],
},
'john_smith.co.uk' => {
'password' => 'DEF296D5-C8BD-3E18-BED2-9BC237D53B27',
'subdomains' => ['@', 'www', 'yetanother', 'makebelieve'],
},
);
while ($loopcnt) {
print "LOOPCNT: $loopcnt\n" if ($TESTING);
$new_ip = get('http://www.whatismyip.com/automation/n09230945.asp');
print "NEW_OP: $new_ip\n" if ($TESTING);
if ($old_ip ne $new_ip) {
$old_ip = $new_ip;
while (my ($domain, $info) = each %domains) {
my $password = $info->{'password'};
my @subdomains = @{$info->{'subdomains'}};
print "\$new_ip='$new_ip', \$domain='$domain',\n" .
"\t\$password='$password',\n" .
"\t\@subdomains=\"@subdomains\"\n\n" if ($TESTING);
UpdateDNS ($new_ip, $domain, $password, @subdomains);
}
}
$loopcnt-- if ($TESTING); # Used to control number of tests during testing.
sleep (120) if ($loopcnt); # Check again in 2 minutes.
}
print "Done\n";
exit;
sub UpdateDNS {
my ($ip, $domain, $password, @subdomains) = @_;
my ($subdomain, $url, $response);
foreach $subdomain (@subdomains) {
$url = 'http://dynamicdns.park-your-domain.com/update?host=' . $subdomain .
'&domain=' . $domain . '&password=' . $password . '&ip=' . $new_ip;
print "URL='$url'\n" if ($TESTING);
$response = get($url);
if ($TESTING) {
$response =~ s/\s+/ /gsi;
$response =~ s/^.+<IP>//;
$response =~ s/<\/IP>.+<Done>/ Done=/;
$response =~ s/<\/Done>.+$//;
print "RESPONSE='$response'\n";
}
sleep (2); # Seems to need a small interval between updates.
}
}
sub daemonize {
# This routine was copied from "Web Mirror: Listing 2" on
# web site "http://www.webreference.com/perl/tutorial/9/3.html".
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
--------------------------------------------------------------------------------------------------
Filename: /etc/init.d/AutoUpdateDynamicDNS
==========================================
#!/bin/sh
#
# AutoUpdateDynamicDNS: Dynamic DNS Updater daemon
#
# chkconfig: 345 87 20
# description: This is a daemon to keep track of the external IP address
# and update the Dynamic DNS address for the hosted web sites
# whenever the IP address changes. (This file is a direct hack
# of the file: /etc/init.d/haldaemon).
#
# It has one annoying bug in that if you carry out a start when
# it has already been started, it creates another instance of
# the daemon.
#
# processname: AutoUpdateDynamicDNS
# pidfile: /var/run/AutoUpdateDynamicDNS.pid
#
# Sanity checks.
[ -x /usr/sbin/AutoUpdateDynamicDNS ] || exit 0
# Source function library.
. /etc/init.d/functions
# so we can rearrange this easily
processname=AutoUpdateDynamicDNS
servicename=AutoUpdateDynamicDNS
pidfile=/var/run/${servicename}.pid
RETVAL=0
# define any local shell functions used by the code that follows
start() {
echo -n "Starting Dynamic DNS Updater daemon: "
daemon --check $servicename $processname
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename
}
stop() {
Adding_an_Automatic_Dynamic_DNS_Updater_Daemon_to_ Fedora-9.txt echo -n "Stopping Dynamic DNS Updater daemon: "
killproc $servicename -TERM
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$servicename
rm -f /var/run/$pidfile
fi
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $processname
RETVAL=$?
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
;;
esac
exit $RETVAL
--------------------------------------------------------------------------------------------------
- 01-27-2009 #2Just Joined!
- Join Date
- Apr 2005
- Location
- Bolton, Lancashire, UK
- Posts
- 3
Adding an Automatic Dynamic DNS Update Daemon to Fedora-9 (completed)
The code for both files are at the end of this writeup.
To use Dynamic DNS on Namecheap.com you need to enable it for your domain(s).
To get it to work properly, you also need to carry out the following commands as root:
chown root:root /usr/sbin/AutoUpdateDynamicDNS /etc/init.d/AutoUpdateDynamicDNS
chmod 750 /usr/sbin/AutoUpdateDynamicDNS /etc/init.d/AutoUpdateDynamicDNS
chkconfig --add AutoUpdateDynamicDNS
chkconfig --level 345 AutoUpdateDynamicDNS on
Then:
To start the daemon:
/etc/init.d/AutoUpdateDynamicDNS start
or
service AutoUpdateDynamicDNS start
To check the daemon's status:
/etc/init.d/AutoUpdateDynamicDNS status
or
service AutoUpdateDynamicDNS status
To stop the daemon:
/etc/init.d/AutoUpdateDynamicDNS stop
or
service AutoUpdateDynamicDNS stop
To restart the daemon:
/etc/init.d/AutoUpdateDynamicDNS restart
or
service AutoUpdateDynamicDNS restart
Cheers
Jim Rey
jimrey1943@googlemail.com
--------------------------------------------------------------------------------------
Filename: /usr/sbin/AutoUpdateDynamicDNS
========================================
#!/usr/bin/perl
# -----------------------------------------------
# Dynamic IP Address updating using Namecheap.com
# -----------------------------------------------
#
# When you enable Dynamic DNS for your domain on Namecheap.com,
# it returns information similar to:
#
# Domain Name johnsmith.com
# Host Name anyhost_you_specify_in_client
# Password AEF296D5-F8BD-2E18-AED2-8BC237D53B26
#
# You iuse the following URL format (url information) to update the IP of your domain:
# http://dynamicdns.park-your-domain.c...%5B&ip=your_ip
#
# For example, in myip.johnsmith.com, myip is the host_name, johnsmith.com is the domain,
# and password will be provided in the dynamic dns page as shown above.
# IP is optional, if you don't specify any IP, the IP from which you are accessing the URL
# will be set for the domain.
# -----------------------------------------------
use LWP::Simple;
use POSIX qw(setsid);
use strict;
# daemonize the program
&daemonize;
my $TESTING = 0; # Set to 1 during testing to print diagnostics
# and limit number of tries.
my ($old_ip, $new_ip, $loopcnt) = ('', '', 10);
my %domains = (
'johnsmith.com' => {
'password' => 'AEF296D5-F8BD-2E18-AED2-8BC237D53B26',
'subdomains' => ['@', 'www', 'anotherip', 'test', 'trial'],
},
'john_smith.co.uk' => {
'password' => 'DEF296D5-C8BD-3E18-BED2-9BC237D53B27',
'subdomains' => ['@', 'www', 'yetanother', 'makebelieve'],
},
);
while ($loopcnt) {
print "LOOPCNT: $loopcnt\n" if ($TESTING);
$new_ip = get('http://www.whatismyip.com/automation/n09230945.asp');
print "NEW_OP: $new_ip\n" if ($TESTING);
if ($old_ip ne $new_ip) {
$old_ip = $new_ip;
while (my ($domain, $info) = each %domains) {
my $password = $info->{'password'};
my @subdomains = @{$info->{'subdomains'}};
print "\$new_ip='$new_ip', \$domain='$domain',\n" .
"\t\$password='$password',\n" .
"\t\@subdomains=\"@subdomains\"\n\n" if ($TESTING);
UpdateDNS ($new_ip, $domain, $password, @subdomains);
}
}
$loopcnt-- if ($TESTING); # Used to control number of tests during testing.
sleep (120) if ($loopcnt); # Check again in 2 minutes.
}
print "Done\n";
exit;
sub UpdateDNS {
my ($ip, $domain, $password, @subdomains) = @_;
my ($subdomain, $url, $response);
foreach $subdomain (@subdomains) {
$url = 'http://dynamicdns.park-your-domain.com/update?host=' . $subdomain .
'&domain=' . $domain . '&password=' . $password . '&ip=' . $new_ip;
print "URL='$url'\n" if ($TESTING);
$response = get($url);
if ($TESTING) {
$response =~ s/\s+/ /gsi;
$response =~ s/^.+<IP>//;
$response =~ s/<\/IP>.+<Done>/ Done=/;
$response =~ s/<\/Done>.+$//;
print "RESPONSE='$response'\n";
}
sleep (2); # Seems to need a small interval between updates.
}
}
sub daemonize {
# This routine was copied from "Web Mirror: Listing 2" on
# web site "http://www.webreference.com/perl/tutorial/9/3.html".
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
--------------------------------------------------------------------------------------
Filename: /usr/sbin/AutoUpdateDynamicDNS
========================================
#!/bin/sh
#
# AutoUpdateDynamicDNS: Dynamic DNS Updater daemon
#
# chkconfig: 345 87 20
# description: This is a daemon to keep track of the external IP address
# and update the Dynamic DNS address for the hosted web sites
# whenever the IP address changes.
#
# processname: AutoUpdateDynamicDNS
# pidfile: /var/run/AutoUpdateDynamicDNS.pid
#
# Sanity checks.
[ -x /usr/sbin/AutoUpdateDynamicDNS ] || exit 0
# Source function library.
. /etc/init.d/functions
# so we can rearrange this easily
processname=AutoUpdateDynamicDNS
servicename=AutoUpdateDynamicDNS
--------------------------------------------------------------------------------------


Reply With Quote