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!

25 Responses to “Automatically updating your IP with DNSimple”

  1. Anthony Eden April 4, 2011 at 10:24 am #

    Thanks for this Jason. I’ve bookmarked it and will link to it from the DNSimple site shortly.

  2. Ben Scheirman April 17, 2011 at 11:26 am #

    jsonip looks pretty cool, but for a bash script it’s not actually that helpful.

    IP = “`curl http://icanhazip.com`” will give you just the IP address in the response body, no need for sed.

    • Jason April 17, 2011 at 11:32 pm #

      Neat site! I didn’t know about it. Very useful.

  3. Spicer Matthews April 23, 2011 at 7:13 pm #

    Thanks for sharing your script. Copy and paste and away I went!!

  4. Steven Haddox May 9, 2011 at 11:19 am #

    Just what the doctor ordered. I hacked away on a few contributions to dyn-dnsimple on Github over the weekend, but it was just too much for such a simple task. This was perfect.

    Thanks!

  5. Glenn Schmidt August 14, 2011 at 10:35 am #

    Nice. Also if you’re already using something like ddclient to update another dyndns service, you can just make it call this script after it’s done (add the ‘postscript’ param to your ddclient.conf). That way you get ddclient’s IP caching (it’ll only call the script when the IP has actually changed, instead of every 15 mins). Also it passes the new IP as the first argument so you can avoid looking it up again.

    Here’s the version I’m using (added a bit of error handling too):

    LOGIN=”"
    PASSWORD=”"
    DOMAIN_ID=”"
    RECORD_ID=”"
    IP=”$1″

    if [ -z "$IP" ]; then
    echo “$0: This script expects an IP address as the first argument” >&2
    exit 1
    fi

    curl -H “Accept: application/json” \
    –basic -u “$LOGIN:$PASSWORD” \
    -H “Content-Type: application/json” \
    -f -s -S -X PUT \
    https://dnsimple.com/domains/$DOMAIN_ID/records/$RECORD_ID.json \
    -d {“record”:{“content”:”$IP”}} >/dev/null

    if [ $? -ne 0 ]; then
    echo “Failed to update dnsimple record with IP $IP” >&2
    exit 2
    fi

    echo “Updated dnsimple domain $DOMAIN_ID record $RECORD_ID with IP $IP”

  6. Jhon James August 26, 2011 at 11:56 am #

    thank you so much for this kind tips, now i am following you on twitter for more updates. !

    thank you again

  7. Antipasto August 26, 2011 at 2:05 pm #

    First i had some problems and i was busting my ass on how to do this…but your script helped me.. THx :)

  8. Fdbooks August 26, 2011 at 11:37 pm #

    awesome, thank you so much :D

  9. Foto Face August 27, 2011 at 6:10 am #

    I use this at all of my sites.Great tip for beginners.

  10. JJ August 27, 2011 at 9:00 am #

    ahh thanks for the script that’s exactly what i was looking for!

  11. Driver Download August 27, 2011 at 2:11 pm #

    Thanks for sharing. It’s very userful for me.

  12. melinda August 27, 2011 at 6:37 pm #

    good news, and thanks for information….

  13. Chatham August 27, 2011 at 7:39 pm #

    looks quite simple. will try.

    thank you for sharing,
    Helmuts

  14. Greg P August 27, 2011 at 10:45 pm #

    I use DNSimple for my hosting as well! They’re a great company with excellent customer service. I never knew this trick though, so I’ll have to give it a shot :)

  15. Pree August 29, 2011 at 1:08 am #

    The abode entertainment landscape is constantly changing, and amazon.com is here to aid you understand those replacements and locate the great products to fit your wants and allocation. don’t know what you need? we can aid you there too

  16. Kitzmiller August 29, 2011 at 1:09 am #

    The abode entertainment landscape is constantly changing, and amazon.com is here to aid you understand those replacements and locate the great products to fit your wants and allocation. don’t know what you need? we can aid you there too.

  17. Cool Piercings August 29, 2011 at 3:06 am #

    I prefer DNSimple lately, too, for DNS and SSL certificates.

  18. Shabby Chic furniture October 9, 2011 at 5:51 am #

    hi Jason,

    have to admit – it is too complicated for me :) … but you can’t ask these knowlwdge from a woman :)

    .. tried to understand the topic, but didn’t get a clue :)

    i suppose, i will be fine – i’m using proffesional hosting company (they should do all this stuff already)

    anyway, good luck,
    Jools

  19. Dustin November 15, 2011 at 3:33 pm #

    Thank you for this bit of script — it was helpful and saved me time. Much appreciated!

  20. Kristopher Murata January 24, 2012 at 11:30 pm #

    Recent jsonip.com update broke this script, because there’s an extra value at the end.

    If you replace this line:

    IP=”`curl http://jsonip.com | sed ‘s/{“ip”:”\(.*\)”/\1/g’ | sed ‘s/}//’`”

    For this one:

    IP=”`curl http://jsonip.com | sed ‘s/{“ip”:”\(.*\)”/\1/g’ | sed ‘s/”, “about”:”\/about}//’`”

    It will work fine again. :)

    • Jason January 25, 2012 at 9:52 am #

      Thanks so much for this! I’ve updated the post.

      • Kristopher Murata January 28, 2012 at 6:16 pm #

        Seems that jsonip.com is playing with their API lately, now there’s no space between the parameters and the script is failing again.

        For a more strict approach, we could do this instead:

        IP=”`curl http://jsonip.com | grep -Eo ‘([0-9]{1,3}\.){3}[0-9]{1,3}’`”

        With grep we are matching any IP address returned by jsonip.com and hopefully they will not start returning two or more IPs! :)

  21. John Deerhake April 4, 2012 at 5:20 pm #

    This was causing 500 errors for me, but I was able to fix them by escaping the quotes in the last line of the curl command:

    -d {\”record\”:{\”content\”:\”$IP\”}}

Trackbacks/Pingbacks

  1. DNSimple | TechRetriever - December 28, 2011

    [...] Dynamic IP Updater in Bash [...]

Leave a Reply