Automatically updating your IP with DNSimple

DNSimple

I’ve been using DNSimple for most a lot of my domain hosting lately. It’s a great service and I highly recommend checking them out for domain hosting. Recently I went out of town but wanted some way to be able to SSH home if I needed to. Luckily, DNSimple has a nice REST API that lets me update records easily. I created a "home" record for one of my domains and created a script to auto update:

#!/bin/bash
 
LOGIN=""
PASSWORD=""
DOMAIN_ID=""
RECORD_ID=""
IP="`curl http://icanhazip.com/"
 
curl -H "Accept: application/json" \
     --basic -u "$LOGIN:$PASSWORD" \
     -H "Content-Type: application/json" \
     -i -X PUT https://DNSimple.com/domains/$DOMAIN_ID/records/$RECORD_ID.json \
     -d {"record":{"content":"$IP"}}

It uses the awesome new jsonip service to grab your ip. It then does a quick sed parsing on that output to grab just your ip. Finally it does a put to the record in DNSimple updating it with the new information. You must have already created a record and domain in order for this to work. I saved this script as dnsimple_update.sh in my ~/bin directory.

Fill in your login and password credentials (or set some environment variables) and domain and record ids and you’re good to go. You can get your domain and record ids by hovering over the edit link in the advanced editor in DNSimple for the record you want and copying and pasting the domain and record ids.

Finally, I set it to run as a cronjob every 15 minutes:

# m h  dom mon dow   command
*/15 * * * * /home/my_user/bin/DNSimple_update.sh

This worked out very well and with some port forwarding on my home router I was able to ssh in to my home machines without any problems.

Update: Kristopher Murata gave a correction to the script in the comments since jsonip changed their format. Twice!! Thanks, Kris!