Skip to content

Instantly share code, notes, and snippets.

@philpennock
Created December 18, 2015 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpennock/c358010a678bed412f30 to your computer and use it in GitHub Desktop.
Save philpennock/c358010a678bed412f30 to your computer and use it in GitHub Desktop.
7 * * * * /usr/local/sbin/update-home-ip cron
#!/bin/bash -eu
progname="$(basename "$0")"
die() { printf >&2 "%s: %s\n" "$progname" "$*" ; exit 1; }
if [[ $# -gt 0 && "$1" == "cron" ]]; then
sleep $((10 + RANDOM % 40 ))
fi
dynhost=CENSORED
dynpass='CENSORED'
myip=$(curl -sS 'https://www.icanhazip.com/')
if [[ ! "${myip:?}" =~ ^[0-9.]+$ ]]; then
die "IP derivation did not give an IP address" "$myip"
fi
# curl -k is insecure, but it's a self-signed cert, just get protection against
# sniffing, not active attacks
set +e
result="$(
curl -k -sS --basic -u "${dynhost:?}:${dynpass:?}" \
"https://dyn.dns.he.net/nic/update?system=dyndns&hostname=${dynhost:?}&myip=${myip:?}"
)"
retval=$?
case $retval in
0) priority=daemon.info ;;
*) priority=daemon.err ;;
esac
logger -p $priority "$progname update of ${dynhost:-?} to ${myip:-?} exited $retval and yielded: $result"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment