Dynamic DNS with a python script

Shane Geiger sgeiger at ncee.net
Thu Dec 27 12:58:04 EST 2007


Marcelo de Moraes Serpa wrote:
> Hello list,
>
> I'd like to set up ssh access to my home computer so that I will be
> able to access it from work - for this no problem, installing sshd is
> straightforward.
>
> The issue here is that I don't have a fixed IP connection at home.
> Also, I wouldn't like to use services such as no-ip or similar.
>
> What I thought was of writing a python program that would detect when
> the internet network interface was restarted/started for the first
> time and then update a DNS record at my DNS server where I would have
> a subdomain that for my home machine's host.
>
> However, I have no idea on how I could detect if the network has been
> restarted nor how I could update this DNS record. Any suggestion would
> be greatly appreciated :)
>
> Thanks in advance,
>
> Marcelo.

How to deal with networking depends on the system you use.  For Debian:

    Debian uses ifupdown (see ifup(8), ifdown(8) and interfaces(5)) to
    manipulate network interfaces. Each interface is provided with
    several scripting hooks: pre-up, up, down, and post-down. These
    hooks are available to each interface as in-line directives in
    /etc/network/interfaces and also as *.d/ directories called with
    run-parts (see run-parts(8)):

       /etc/network/if-up.d/
       /etc/network/if-pre-up.d/
       /etc/network/if-down.d/
       /etc/network/if-post-down.d/




SETTING UP A DOMAIN NAME WITH DYNDNS.ORG

Go to their site and set up an account.  While doing that (or perhaps
after--I don't remember), you can choose a domain name.  You *could*
think of what they are giving you is a subdomain--not actually a
domain.  You will be able to choose something like foo.mine.nu where
"foo" is the name you choose for the name of your subdomain and
"mine.nu" is the domain they already own.  (You can choose from many
domains they already own.)  Since they manage these domains themselves,
they do not have to pay a registrar to set up a subdomain...and
therefore they can give you a subdomain for free.  (Actually, I'm using
the term "subdomain" loosely here...but I'm sure you get the point I'm
trying to make--which is that you don't have to pay for this name.)

Once you have your name (foo.mine.nu, for example), you can set up a
program on your computer to update their DNS servers with your new
domain name whenever it changes.

--------------------------------------------------


SETTING UP IPCHECK WITH DYNDNS.ORG

I have been satisfied with my first choice (dyndns.org), so I haven't
tried the others.  They probably work well, too.

Set up an account on dyndns.org, and record the username, password, and
domain name in your notes.  In the following examples, I use the
following values:

username:  sgeiger
pass:  asdfasdf
domain name:  sgeiger.mine.nu


apt-get install ipcheck

You might want to run this first so that you are in the directory cron
will use when you run ipcheck:
sudo su; cd ~;

HERE'S HOW TO CONFIGURE IT:
sgeiger at minime:~$ sudo /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu
ipcheck.py: No ipcheck.dat file found.
ipcheck.py: Use same command+options ONCE with --makedat to create from
DNS lookup.
sgeiger at minime:~$ sudo /usr/sbin/ipcheck.py --makedat -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu
ipcheck.py: ip1 looking up sgeiger.mine.nu
ipcheck.py: result: 'sgeiger.mine.nu'[]['139.55.232.72']
sgeiger at minime:~$ sudo /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine
.nu
sgeiger at minime:~$ sudo /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu
sgeiger at minime:~$ sudo /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu
sgeiger at minime:~$

When you run this manually and it works you will see no output.

To add a cron job to update this automatically once a day at 10 am:

###  Note:  This script should be run once a day from cron, like this
(which runs it at 10:15 am every day)
15 10 * * * /usr/bin/python /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu


To add another names (foobar.mine.nu):
sudo /usr/sbin/ipcheck.py --makedat -l -r checkip.dyndns.org:8245
shanerg rQ9ll0 sgeiger.mine.nu,foobar.mine.nu

...and then put this into the cron tab:
15 10 * * * /usr/bin/python /usr/sbin/ipcheck.py -l -r
checkip.dyndns.org:8245 shanerg asdfasdf sgeiger.mine.nu




Check the mail messages on the computer you are using in case there were
errors.

Note:  This creates a file named /root/ipcheck.dat and /root/ipcheck.log
on my computer.




STEPS FOR ADDING ANOTHER HOST:
  - add the host on dnydns.org
  - run the script with makedat option
  - run the script to update the server

In the example below, ipcheck will update the dyndns.org servers for the
names "sgeiger.mine.nu" and for "myothername.mine.nu":

sgeiger at minime:/root$ sudo /usr/sbin/ipcheck.py --makedat -l -r
checkip.dyndns.org:8245 shanerg asdfasdf
sgeiger.mine.nu,sgeigerbot.mine.nu,myothername.mine.nu
ipcheck.py: There is already an ipcheck.dat file existing.
ipcheck.py: Remove this file first before running --makedat
sgeiger at minime:/root$ sudo mv ipcheck.dat ipcheck.dat-old
sgeiger at minime:/root$ sudo /usr/sbin/ipcheck.py --makedat -l -r
checkip.dyndns.org:8245 shanerg asdfasdf
sgeiger.mine.nu,sgeigerbot.mine.nu,myothername.mine.nu
ipcheck.py: ip1 looking up sgeiger.mine.nu
ipcheck.py: result: 'sgeiger.mine.nu'[]['139.55.233.228']
ipcheck.py: ip2 looking up sgeigerbot.mine.nu
ipcheck.py: result: 'sgeigerbot.mine.nu'[]['139.55.233.228']
ipcheck.py: ip2 looking up myothername.mine.nu
ipcheck.py: result: 'myothername.mine.nu'[]['139.55.233.228']
sgeiger at minime:/root$


sgeiger at minime:/nu/mine/myothername$ sudo crontab -l
15 10 * * *     /usr/sbin/ipcheck.py -l -r checkip.dyndns.org:8245
shanerg asdfasdf sgeiger.mine.nu,sgeigerbot.mine.nu,myothername.mine.nu
--acctfile /root/ipcheck.dat
sgeiger at minime:/nu/mine/myothername$







-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy




More information about the Python-list mailing list