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.

#!/bin/sh
 
# By Ed Wiget
# updates a subdomain to point to my home ip address
# myserver.mydomain.com A YOUR.HOME.IP.ADDRESS
 
# get your linode api key under your account user section
LINODE_API_KEY=
 
# you can get all the domain ids which must be numeric by running
# for all domains
# curl -s https://api.linode.com/?api_key="$LINODE_API_KEY"\&api_action=domain.list
# for a specific domain
# curl -s https://api.linode.com/?api_key="$LINODE_API_KEY"\&api_action=domain.list\&DomainID="170426"
DOMAIN_ID=
# then you get the resource id for the domain by using
# curl -s https://api.linode.com/?api_key="$LINODE_API_KEY"\&api_action=domain.resource.list\&DomainID="170426"
RESOURCE_ID=
 
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
fi
 
# set our home directory which holds our ip file
HOMEDIR=/home/your_username
 
# 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 http://www.edwiget.name/ip.php`
	# 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
		else
 			echo "Updating DNS to $WAN_IP"
			curl -s https://api.linode.com/?api_key="$LINODE_API_KEY"\&api_action=domain.resource.update\&DomainID="$DOMAIN_ID"\&ResourceID="$RESOURCE_ID"\&Target="$WAN_IP" > /dev/null
 			touch ~/.last-linode-ddns-refresh
		fi
else
# this must be a new setup
 
	# grab the current ip
	WAN_IP=`curl -s http://www.edwiget.name/ip.php`
        # and populate the new file
        echo ${WAN_IP} > ${HOMEDIR}/.ddns
	# and update dns
	curl -s https://api.linode.com/?api_key="$LINODE_API_KEY"\&api_action=domain.resource.update\&DomainID="$DOMAIN_ID"\&ResourceID="$RESOURCE_ID"\&Target="$WAN_IP" > /dev/null
        touch ~/.last-linode-ddns-refresh
 
fi

Copy the above code to /usr/local/bin/update.linode.sh  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/update.linode.sh > /dev/null 2>&1 | logger "linode dynamic dns update complete"