2012-05-20

Python FreeDNS client (on Raspberry Pi)

I'm using FreeDNS - a dynamic DNS provider for my Raspberry Pi. If you sign up and follow the instructions on their site you can quickly have a nice name for your site like mine: Jerbly

To keep your external dynamic address updated with FreeDNS there are a number of clients (scripts) available. Since this is Raspberry Pi though I had to have one in Python.
#!/usr/bin/python

import urllib2
import os.path

OLDIP_FILE = '/var/lib/misc/oldip'

def updatedns(ip):
    print urllib2.urlopen("http://freedns.afraid.org/dynamic/update.php?YOUR-SECRET-KEY-HERE").read().strip()
    f = open(OLDIP_FILE, 'w')
    f.write(ip)
    f.close()

newip = urllib2.urlopen("http://ip.dnsexit.com/").read().strip()

if not os.path.exists(OLDIP_FILE):
    updatedns(newip)
else:
    f = open(OLDIP_FILE, 'r')
    oldip = f.read()
    f.close()
    if oldip != newip:
        updatedns(newip)

NOTE: You will have to change the YOUR-SECRET-KEY-HERE to the one supplied by FreeDNS.

I've just put this script (called simply "freedns") in my /etc/cron.hourly directory which is good enough for my site.

4 comments:

dicsEE said...

Just what i was looking for. Didn't know you can update the ip on afraid.org just by using that URL.
Thanks !

Anonymous said...

I'm doing nearly the same thing with my Raspberry Pi, using it to update dynamic IP. However I'm using www.no-ip.org services. There is an appropriate arch linux package to help you to do it automatically.

Cheers,
vova

André Ricardo said...

I am using inadyn with the following config

--username my_user
--password my_pass
--alias my_domain.uk.to,hash
--dyndns_system default@freedns.afraid.org
--iterations 1
--syslog

Then added it to the cron, it uses even less memory
@reboot /usr/sbin/inadyn
*/5 * * * * /usr/sbin/inadyn

Mike D said...

How about just using wget in the crontab file? I'm doing that on my Raspberry Pi and it works well.

$crontab -e

And then add to the file:

@reboot wget
*/5 * * * * wget
*/15* * * * rm ~/update*

The last line cleans up the update files that wget retrieves. Save the file and you're good to go.