Créer le script
/usr/local/sbin/update_ip :
#!/bin/bash
DOMAIN="domain.tld"
HOSTNAME="host.${DOMAIN}."
NS_SRV=ns0.domain.tld
KEY="/etc/bind/Khost.domain.tld.+XXXXX.private"
LOG='/var/log/update_ip.log'
DEBUG=0
[ "$1" == "-d" ] && DEBUG=1
FORCE=0
[ "$1" == "-f" ] && DEBUG=1 && FORCE=1
cur=$( host $HOSTNAME $NS_SRV|tail -n 1|awk '{print $4}' )
[ $DEBUG -eq 1 ] && echo "Current in DNS : $cur"
new=$( wget -q -O - http://ip.zionetrix.net )
if [ $( echo $new|egrep -c '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ) -ne 1 ]
then
echo "$( date ) - Fail to get IP (output : $new)"|tee -a $LOG
exit 1
fi
[ $DEBUG -eq 1 ] && echo "Current in real : $new"
if [ "$cur" != "$new" -o $FORCE -eq 1 ]
then
echo "$( date ) - IP Change : $cur => $new"|tee -a $LOG
echo "$cur => $new"
echo "$( date ) - Update IP :"|tee -a $LOG
tmp=$( mktemp )
cat > $tmp << EOF
server $NS_SRV
zone $DOMAIN
update delete $HOSTNAME A
update add $HOSTNAME 180 A $new
show
send
EOF
echo -e "$( date ) - Generated script to change IP via nsupdate :\n\n$(cat $tmp )\n"|tee -a $LOG
echo -e "$( date ) - Run nsupdate :"|tee -a $LOG
nsupdate -k "$KEY" -v $tmp|tee -a $LOG
fi