Dec 042013

This is somewhat related to updating amazon group resource ip’s for dynamic ip addresses except it is a different concept.  How many times have you been on the road and needed to access your home computer?  Granted, there are many third party services that allow you to do that, like dynamic dns but that is behind my control.  I wanted something I could control.  Since I use linode, they have an api and a way to script ip updates.  So thats what we will do here.

Again, this is not rocket science.  We simply create a sub-domain for an existing domain in Linodes DNS manager, and then create a script that will update that record each time our home ip address (or any other computer, server, device, etc) changes its ip address.

The script is pretty self explanatory.  If you have questions, just ask.

# By Ed Wiget
# updates a subdomain to point to my home ip address
# get your linode api key under your account user section
# you can get all the domain ids which must be numeric by running
# for all domains
# curl -s"$LINODE_API_KEY"\&api_action=domain.list
# for a specific domain
# curl -s"$LINODE_API_KEY"\&api_action=domain.list\&DomainID="170426"
# then you get the resource id for the domain by using
# curl -s"$LINODE_API_KEY"\&api_action=domain.resource.list\&DomainID="170426"
if [ "${LINODE_API_KEY}" = "" -o "${DOMAIN_ID}" = "" -o "${RESOURCE_ID}" = "" ]; then
    echo "you need to insert your linode api key, domain id, and resource id in the variables of this script"
    exit 1
# set our home directory which holds our ip file
# first we check for existing file
if [ -f ${HOMEDIR}/.ddns ]; then
	# if it exists, we create a backup for comparison
	cp ${HOMEDIR}/.ddns ${HOMEDIR}/.ddns.old
	# then grab the current ip
	WAN_IP=`curl -s`
	# and populate the new file
	echo ${WAN_IP} > ${HOMEDIR}/.ddns
	# here we need to check if the files differ
	diff ${HOMEDIR}/.ddns ${HOMEDIR}/.ddns.old
		if [ $? = 0 ]; then
			echo "no update required"
			exit 1
 			echo "Updating DNS to $WAN_IP"
			curl -s"$LINODE_API_KEY"\&api_action=domain.resource.update\&DomainID="$DOMAIN_ID"\&ResourceID="$RESOURCE_ID"\&Target="$WAN_IP" > /dev/null
 			touch ~/.last-linode-ddns-refresh
# this must be a new setup
	# grab the current ip
	WAN_IP=`curl -s`
        # and populate the new file
        echo ${WAN_IP} > ${HOMEDIR}/.ddns
	# and update dns
	curl -s"$LINODE_API_KEY"\&api_action=domain.resource.update\&DomainID="$DOMAIN_ID"\&ResourceID="$RESOURCE_ID"\&Target="$WAN_IP" > /dev/null
        touch ~/.last-linode-ddns-refresh

Copy the above code to /usr/local/bin/  and make it executable.

Then we simply create a crontab to run the script however often you want to check your ip changed or not.

# this updates dynamic dns info at linode
*/30 * * * * /usr/local/bin/ > /dev/null 2>&1 | logger "linode dynamic dns update complete"

This site uses Akismet to reduce spam. Learn how your comment data is processed.