Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (86 sloc) 3.68 KB
#!/bin/sh
# Get current IP from checkip.dyndns.org, filter out current WAN IP, check
# against cached value; if different, log, send email, and update DynDNS
# (dyndns.org).
# 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.
#myid="username:userpass"
#myhost="myhostname.dyndns.info"
# By default we use one of the test accounts. Set your real id and host
# above and comment these out!
myid="test:test"
myhost="test.merseine.nu"
# If you want email notification of IP address changes _and_ you have an
# MTA running on this machine, set this:
myemail=
### End of configuration
update_dyndns () {
reason=$1
# keep is deprecated in the API, so we leave it out
#keep="&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
keep=
svc="members.dyndns.org/nic/update"
agent="http://notes.nimblemachines.com/show/DynamicDnsUpdater"
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
fi
# Only update currentip and expires if update (via dyndns.org) 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
fi
}
# The IP address part of the response from checkip.dyndns.org (an HTML
# page) looks like this: "Current IP Address: 71.34.77.71"
# Load the page separately from parsing out the IP address, so it's easier
# to debug problems, if any.
curl -o checkip.html http://checkip.dyndns.org/
#currentip=$(cat checkip.dyndns.org.html \
currentip=$(cat checkip.html \
| sed -n -e 's/\(.*Current IP Address: \)\([0-9.]\{1,\}\)\(.*\)/\2/p')
# get IP from last run
lastip="0.0.0.0"
[ -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
expires=0
[ -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 checkip.dyndns.org, 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"
fi