The previous script some times hangs, specially when no internet connection is available (Network is unreachable or unable to resolve host address). This one takes care of these problems.
#!/bin/sh
# FILE: dhcp3/dhclient-exit-hooks.d/zzz_public_ip
username=*fill in*
password=*fill in*
domain=*fill in*
function test_network_and_ip_on_dns() {
# case `ping -qnc 1 google.com 2>&1` in
# *'100% packet loss'*)
# logger -t publicIP “The network is DOWN.”
# exit 0
# ;;
# esac
TEST=`wget --timeout=10 --tries=1 -O -
www.google.com 2>&1`
TEST_1=`echo $TEST | grep "Network is unreachable"`
TEST_2=`echo $TEST | grep "unable to resolve host address"`
# echo "NETSTATUS: $NETSTATUS."
echo "test: $TEST"
echo "test_1: $TEST_1"
echo "test_2: $TEST_2"
# Check that network is up.
if [ ! -z "$TEST_1" ] || [ ! "" == "$TEST_1" ] || [ ! -z "$TEST_2" ] || [ ! "" == "$TEST_2" ]; then
logger -t publicIP “Network is DOWN.”
exit 0
fi
# Only if none of them is set then network is up.
test_ip_on_dns
}
function test_ip_on_dns() {
dns_ip=`host "$domain" |sed -e 's/[A-Za-z. \-]* //'`
actual_ip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
if [ "$dns_ip" == "$actual_ip" ]; then
logger -t publicIP "Public IP is $actual_ip"
else
logger -t publicIP "Updating ip for domain $domain with username $username"
/usr/sbin/inadyn --iterations 1 -u $username -p $password -a $domain
logger -t publicIP "Public IP is $actual_ip"
fi
}
if [ -z "$reason" ]; then
test_network_and_ip_on_dns
exit 0
fi
case "$reason" in
MEDIUM|ARPCHECK|ARPSEND|NBI|BOUND|RENEW|REBIND)
logger -t publicIP "Updating public IP. Reason: $reason"
test_network_and_ip_on_dns
;;
EXPIRE|FAIL|TIMEOUT|PREINIT|REBOOT|STOP|RELEASE|*)
logger -t publicIP "Not updating. Reason: $reason"
;;
esac