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.
Post a Comment