adventures in ddns part 4

In this part, I'm going to use Python and XMLRPC to update a clients IP address. The end result is a sort of DIY Dyndns system.

Both the client and server parts of XMLRPC are extremely easy to implement in Python. Let's start with the server:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from ddns import nsupdate

s = SimpleXMLRPCServer(("", 4242))

def ipregister(ip, domain):
    config = {}
    config['nscmd'] = "/usr/bin/nsupdate"
    config['server'] = "127.0.0.1"
    config['timeout'] = "36000"
    config['keyfile'] = "/path/to/keyfile"

    ns = nsupdate(config)
    ns.addDomain(domain, 'A %s' % ip)

s.register_function(ipregister)

I'm using the ddns script and dnspython package from back in Part 3. As you might be able to tell from the code, I'm setting up a simple server on port 4242 with one function: ipregister. This function takes an IP address and a hostname as an argument. It then configures the nsupdate script and tells it about the received information. nsupdate tells DNS of the changes and our hostname is now registered.

And now for the client:

from xmlrpclib import Server

s = Server('http://localhost:4242')
ip = get_my_ip_somehow()
s.ipregister(ip, 'myhostname.example.com'):

The client script is even more simple than the server. All it's doing is configuring itself as an XMLRPC client, grabbing an IP address somehow, then calling the ipregister function with the IP and a chosen hostname.

This is a very, very basic system. I do no authentication or checks to see if I even host the zone, but this will do just fine for example purposes. Basically, all you need to do now is have the client check in every few hours with it's IP address and you're good to go with your own dyndns system!

Tags: , , , , , ,