Using Linode Dynamic DNS with OpenWRT

If you have a VPS at Linode, then you have free Dynamic DNS. You can use that from a regular Linux machine, or from a router that runs OpenWRT.

This article describes how to setup an OpenWRT router with Linode Dynamic DNS.

First, the script itself, which goes into the file: /etc/custom/linodedns.sh

#!/bin/ash
 
BASE=`basename $0 .sh`
DIR=`dirname $0`

OUTPUT1=/tmp/$BASE-1.out
OUTPUT2=/tmp/$BASE-2.out

# Read the configuration file
CONF=$DIR/$BASE.conf
if [ -r "$CONF" ]; then
  . $CONF
else 
  logger -t $BASE "Could not read configuration file: $CONF"
  exit 1
fi

# External Ethernet interface
IFACE=`uci get network.wan.ifname`

# Get the current external IP address
CURRIP=`ifconfig $IFACE | grep 'inet addr' | awk '/inet addr/ {print $2}' | awk -F: '{print $2}'`

# Get the currently configured IP address in Linode's DNS
curl -sk "https://api.linode.com/?api_key=$APIKEY&action=domain.resource.list&ResourceID=$RESOURCEID&DomainID=$DOMAINID" > $OUTPUT1
RC=$?
if [ "$RC" != 0 ];then 
  logger -t $BASE "curl list reported error: $RC"
  exit 2
fi

# Parse the output for the IP address
DNSIP=`cat $OUTPUT1 | sed -e 's/^.*"TARGET":"//' -e 's/",.*$//'`

if [ "$DNSIP" = "$CURRIP" ]; then
  logger -t $BASE "External IP address has not changed. No IP update necessary. IP address: $CURRIP"
  exit 0
fi

curl -sk "https://api.linode.com/?api_key=$APIKEY&action=domain.resource.update&ResourceID=$RESOURCEID&DomainID=$DOMAINID&Target=$CUR
RIP" > $OUTPUT2
RC=$?
if [ "$RC" != 0 ];then 
  logger -t $BASE "Curl update reported error: $RC"
  exit 2
fi

# Log the IP address change
logger -t $BASE "Updated IP address from $DNSIP to $CURRIP"

Then you have to create a configuration file, with the same name as the script, except that instead of .sh its suffix is .conf, for example: /etc/custom/linodedns.conf. You need to get the following values and place them in your configuration file.

APIKEY="your-api-key-goes-here"
RESOURCEID="resource-id-goes-here"
DOMAINID=domainid

Finally, have the script run automatically 3 times per hour, from cron. Use crontab -e, or "Scheduled Tasks" from LuCI, and add the following lines.

# Check external IP address, and update Linode's DNS if changed
5,25,45 * * * * /etc/custom/linodedns.sh

That is it. Now your public IP address will be updated no later 20 minutes if it changes!

Contents: 

Tags: