Friday, November 28, 2014

FreeDNS Cron Update

A Dynamic DNS Bash Script for FreeDNS
First, make sure you have registered an account for FreeDNS, and set up a subdomain as an A record at

Save the contents below as, making sure to change /path/to/log/dns.log , <YOUR_API_KEY> , <YOUR_SUBDOMAIN>. You can get both your API Key and the domain from . The domain should be something like (or or one of the other FreeDNS domains).

You can get the full update line (https address and the full wget call, seen in the if statement below) from your FreeDNS dashboard, under Main Menu -> Dynamic DNS -> quick cron example , toward the bottom of the page.

echo "-----------------------" >> $LOG                                          
echo "Running" >> $LOG                                                   
echo $(date) >> $LOG                                                            
# get the current ip...                                                         
CURRENT_IP=$(wget -q -O -|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
echo "Current IP:"$CURRENT_IP >> $LOG                                           
#FreeDNS updater script                                                         
PREVIOUS_IP=$(nslookup $SUBDOMAIN | tail -n2 | grep A | sed s/[^0-9.]//g)                
echo "Previous IP:"$PREVIOUS_IP >> $LOG                                         
if [ "$CURRENT_IP" != "$PREVIOUS_IP" ]                                          
        echo "Current and previous IP differ! Updating FreeDNS..." >> $LOG      
        wget -q -O /dev/null $UPDATE_URL                                         
        echo "DNS updated on:"$(date) >> $LOG                                   
echo "-----------------------" >> $LOG

Now run sudo vim /etc/crontab , adding this line at the bottom,
replacing /path/to/script/ with the path you saved to,
and replacing <USER> with your username
*/5 * * * * <USER> /path/to/script/

No comments:

Post a Comment