#! /bin/bash
# This script updates a dynamic-DNS record for a specific NIC of your host.
# Script is made for freedns.afraid.org but should in principle work for
# other providers as well that allow DNS updates by simply loading a
# specific URL.
#
# Usage: dyndns_update.sh IF SHA1
#
# As this script is meant to be run from cron, it will be fairly silent,
# except when something goes wrong in an unexpected way or when a change of
# IP address actually occurred. (note, some of the fails are normal and not
# unexpected). To see the output of the log, export the environment variable
# LOGFILE to "--" (any other content of that variable will write to this
# file-name).
#
# Example: (export LOGFILE="--"; dyndns_update.sh eth1 $MYHOST)
                                                                                    
IFNAME=${1}                                                                         
SHA1=${2}                                                                           
SHOST=${3-"freedns.afraid.org"}                                                     
SHELL=$(basename $0)                                                                
                                                                                    
#Set export of this to "--" if you want output visible                              
LOGFILE=${LOGFILE-"/tmp/update_dyndns.log"}                                         
source logger.sh                                                                    
source pipe_logic.sh                                                                
                                                                                    
if [ "X$IFNAME" == "X" ]; then                                                      
        logger "First argument (IFNAME) must be present. Exiting..."                
        exit 1                                                                      
fi                                                                                  
                                                                                    
if [ "X$SHA1" == "X" ]; then                                                        
        logger "Second argument (SHA1) must be present. Exiting..."                 
        exit 1                                                                      
fi                                                                                  
                                                                                    
NIC_IPNR=$(ifconfig | \                                                             
        grep $IFNAME -A3 | \                                                        
        grep "inet addr" | \                                                        
        cut -f2 -d":" | \                                                           
        cut -f1 -d " ")                                                             
LNET=$(echo $NIC_IPNR | cut -f1-3 -d".").0                                          
GW=$(echo $NIC_IPNR | cut -f1-3 -d".").1                                            
URL="http://${SHOST}/dynamic/update.php?${SHA1}"                                    
                                                                                    
if [ "X$NIC_IPNR" == "X" ]; then                                                    
        logger "IF [$IFNAME] not up or doesn't exist. Exiting..."                   
        exit 1                                                                      
fi                                                                                  
                                                                                    
logger "$(basename $0): Updating dynamic DNS to interface: [$IFNAME]"               
logger "   NIC: [$NIC_IPNR]"                                                        
logger "   NET: [$LNET]"
logger "    GW: [$GW]"
logger "   URL: [$URL]"
logger

# Try removing temporary route first, in case last invocation didn't complete.
route -v del -host $SHOST >/dev/null 2>&1

#set -e  #Trigger e-mail if something goes wrong from here on & if run in cron
logger "Setting temporary route for [$SHOST]"
route -v -A inet add -host $SHOST gw $GW | logger
#route -v -A inet add -host $SHOST gw $GW
netstat -anr | logger
#netstat -anr
logger "Updating dyn-DNS [$URL]"
RESPONSE=$(wget -O - $URL 2>/dev/null)
logger "Removing temporary route [$SHOST]"
route -v del -host $SHOST | logger
logger
logger "Response from service provider [$SHOST] for [$IFNAME]:"
logger "$RESPONSE"
set +e  #Stop trigger error mails (grep returns fail is not found)
if [ \
        "X$(echo $RESPONSE | grep 'has not changed')" == "X" -a \
        "X${LOGFILE}" != "X--" \
]; then
        # Something else is responded. Make it visable in syslog (if used from
        # cron), might be an error (or at least interesting).
        echo "Response from service provider [$SHOST] for [$IFNAME]:"
        echo "$RESPONSE"
fi