Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (86 sloc) 3.68 KB
# Get current IP from, filter out current WAN IP, check
# against cached value; if different, log, send email, and update DynDNS
# (
# So I'm going to give up the "responsiveness" of checking the IP every
# minute and use dyndns's checkip service, which can't be polled more
# frequently than once every 10mins. Oh well. At least it'll be more
# reliable!
### Configuration
# This is run from cron, so make sure that we can find sed, curl, mail and
# logger.
export PATH=/usr/local/bin:$PATH
# The scripts write a couple of files here. If you're running from cron as
# a regular user, this directory needs to be readable and writable by that
# user.
cd $scriptdir
# This is whatever you set up when you opened your DynDns account.
# By default we use one of the test accounts. Set your real id and host
# above and comment these out!
# If you want email notification of IP address changes _and_ you have an
# MTA running on this machine, set this:
### End of configuration
update_dyndns () {
# keep is deprecated in the API, so we leave it out
response=$(curl --cacert cacert_mozilla.pem \
-A $agent "https://$myid@$svc?hostname=$myhost&myip=$currentip$keep")
#logger -t "update-ip" "$reason: $lastip --> $currentip"
#logger -t "update-ip" "dyndns responded: $response"
(echo "$stamp $reason: $lastip --> $currentip"
echo "$stamp dyndns responded: $response") >> log
if [ "$myemail" ]; then
echo "$response" | mail -s "$myhost ($reason): $lastip --> $currentip" $myemail
# Only update currentip and expires if update (via was
# successfull.
if echo $response | grep -q "good\|nochg"; then
echo "$stamp good response from dyndns; resetting lastip and expires" >> log
# remember our currentip
echo $currentip > lastip
# record our "expires" time, 28 days in the future
# 28 days is 86400 * 28 = 2419200 seconds
echo $(($now + 2419200)) > expires
# The IP address part of the response from (an HTML
# page) looks like this: "Current IP Address:"
# Load the page separately from parsing out the IP address, so it's easier
# to debug problems, if any.
curl -o checkip.html
#currentip=$(cat \
currentip=$(cat checkip.html \
| sed -n -e 's/\(.*Current IP Address: \)\([0-9.]\{1,\}\)\(.*\)/\2/p')
# get IP from last run
[ -f lastip ] && lastip=$(cat lastip)
# Get the current time, in seconds since the Unix epoch (UTC)
now=$(date +%s)
# Get our expires time, if it exists
[ -f expires ] && expires=$(cat expires)
# Make a note that we've run, and what the current values are
stamp=$(date "+%F %T")
echo "$stamp $lastip --> $currentip ($(($expires-$now)) seconds remain)" >> log
# If we didn't get a valid IP address from, don't do
# anything more.
if [ -z "$currentip" ]; then
echo "$stamp No valid IP address returned from checkip; exiting" >> log
# If, by some miracle, 28 days have elapsed _and_ our IP address has
# changed, don't do both updates.
elif [ $now -ge $expires ]; then
update_dyndns "28 days passed"
# Make sure we got an IP before we blow away our old one and contact
# dyndns. We don't want to get blocked for abuse.
elif [ "$currentip" -a "$currentip" != "$lastip" ]; then
update_dyndns "WAN IP changed"