Friday, 4 April 2014

Setting up an OVH dynhost (dynamic DNS) on OpenWrt

This is the way I followed to add a dynamic DNS on my TPlink TL-WR703N.

Step 1: Install updatedd and the OVH plugin

opkg update ; opkg install updatedd updatedd-mod-ovh

Step 2: Copy the script in (e.g.) /root, mind the permissions

The script below is a modified version of the one I found here. The modified version uses SH instead of BASH and WGET instead of W3M to avoid having to install additional packages on the router:
#!/bin/sh

## CONFIGURATION ##

# Connection to OVH DynHost
username=USERNAME_HERE
password=PASSWORD_HERE
host=FULL.DOMAIN.NAME.HERE

# How to log
# 1 = true, 0 = false
log_change=1
log_no_change=0
log_file=/var/log/dynhost.log

# File with old IP
old_ip_file=/var/cache/ip_old

# To avoid error "file not found"
mkdir -p /var/cache
touch ${old_ip_file}
touch ${log_file}

# Get Public IP
ip=`wget -q -O - http://www.monip.org/ | awk -F': ' '/IP/ { print $2 }' | sed 's_<.*$__g' | grep .`

# Get Old Public IP
ip_old=`cat ${old_ip_file}`

# Test if IPs are equal
if [ "${ip}" = "${ip_old}" ]
then
if [ "${log_no_change}" = "1" ]
then
 echo `date`: No IP change was found >> ${log_file}
fi 
else
 echo ${ip} > ${old_ip_file}
 if [ "${log_change}" = "1" ]
 then
  echo "`date`:IP has change. (Old : ${ip_old}, New : ${ip})" >> ${log_file}
  updatedd ovh -- --ipv4 ${ip} ${username}:${password} ${host} >> ${log_file}
 else
  updatedd ovh -- --ipv4 ${ip} ${username}:${password} ${host}
 fi
fi
Update the permission for execution:

chmod +x /root/updateDynhost

Step 3: Open crontab to add the line calling our script

crontab -e

Step 4: add the line to run the command every 10mins

Press 'i' to insert the line:
0,10,20,30,40,50 * * * * /root/updateDynhost

Step 5: Quit vi (used to edit the crontab)

Press [ESC], type ':wq' (without the quotes), press [ENTER]

Step 6: Enable and start cron

/etc/init.d/cron enable
/etc/init.d/cron start

Done !