04. März 2014

Dynamic DNS with DNSimple

I have been using DNSimple as my DNS Provider of choice for a while now. Instead of hosting DNS entries for my domains with different registrars or webhosters I have one simple spot to manage subdomains and other domain related stuff.

In a Ruby on Rails app I am currently involved in, we are working with subdomains to separate tenants. Working with subdomains on localhost is not a real problem as you can set hostnames in your local /etc/hosts file or use http://subdomain.lvh.me which points to localhost. But what if you have to test stuff on an iPad or on one of those dusty old Windows machines?

To solve this I’ve set a new A record in the DNSimple web ui which points to my internal 192.168.x.x address and a corresponding wildcard CNAME for subdomains. Now I can use the URL http://tenant.macbook.neckhair.ch:3000 to point the iPad’s browser to my development server. This works great until the DHCP server on the router decides to assign me a new ip address.

Therefore I decided to write a little script that updates the dns entries regularly via the DNSimple API.

DOMAIN_TOKEN="<domain_token>"
DOMAIN="neckhair.ch"
RECORD_ID="<record_id>"
IP="`ifconfig | grep "inet " | grep -v 127.0.0.1 -m 1 | cut -d\  -f2`"
<p>curl -H "X-DNSimple-Domain-Token: $DOMAIN_TOKEN" \
     -H "Accept: application/json" \
     -H "Content-Type: application/json" \
     -X PUT "https://dnsimple.com/domains/$DOMAIN/records/$RECORD_ID" \
     -d '{"record":{"content":"'$IP'"}}'

Thanks to cron my dns record now gets updated every hour.