Skip to content

Instantly share code, notes, and snippets.

@hraban
Last active June 1, 2018 19:39
Show Gist options
  • Save hraban/99e8f7aab2eb18fafa024e6db71a9c82 to your computer and use it in GitHub Desktop.
Save hraban/99e8f7aab2eb18fafa024e6db71a9c82 to your computer and use it in GitHub Desktop.
Fork of Easydns dyndns update script
#!/bin/bash
set -eu -o pipefail
#################################################################
## ChangeIP.com bash update script ##
#################################################################
## Written 3/18/09 by Tom Rinker, released to the Public Domain##
#################################################################
## This is a simple bash script to preform a dDNS update with ##
## ChangeIP.com. It uses only bash and wget, and so should be ##
## compatible with virtually any UNIX/Linux based system with ##
## bash. It is intended to be executed as a cron job, and ##
## will only execute an update of dDNS records when the IP ##
## address changes. As ChangeIP.com dDNS records have a 5 min ##
## Time-To-Live, it is basically pointless and wasteful to ##
## execute it more often than every 5 minutes. This script ##
## supports logging all activity, in 3 available log levels, ##
## and supports simple management of log file size. ##
#################################################################
## To use this script: ##
## 1) set the variables in the script below ##
## 2) execute the script as a cron job ##
#################################################################
## WARNING: This script has two potential security holes. ##
## First, the username and password are stored plaintext in ##
## the script, so a system user who has read access to the ##
## script could read them. This risk can be mitigated with ##
## careful use of file permissions on the script. ##
## Second, the username and password will show briefly to other##
## users of the system via ps, w, or top. This risk can be ##
## mitigated by moving the username and password to .wgetrc ##
## This level of security is acceptable for some installations ##
## including my own, but may be unacceptable for some users. ##
#################################################################
################ Script Variables ###############################
# Set auth in ~/.netrc for nic.changeip.com
# Set CIPHOST here or in envvar
# CIPHOST=...
: ${CIPHOST?Required envvar CIPHOST not set}
IPPATH=/var/log/IP # IP address storage file
TMPIP=/tmp/tmpIP # Temp IP storage file
LOGPATH=/var/log/changeip.log # Log file
TEMP=/tmp/temp # Temp storage file
LOGLEVEL=2 # 0=off,1=normal,2=verbose
LOGMAX=500 # Max log lines, 0=unlimited
#################################################################
useragent="rinker.sh wget 1.0"
# get current IP from ip.changeip.com, and store in $TMPIP
curl --fail -sSL -A "$useragent" http://ip.changeip.com | head -1 > "$TMPIP"
# compare $IPPATH with $TMPIP, and if different, execute update
if diff $IPPATH $TMPIP > /dev/null
then # same IP, no update
if [ $LOGLEVEL -eq 2 ] ; then # if verbose, log no change
(
echo "--------------------------------"
date
echo "No Change"
echo -n "IP: "
cat $IPPATH
echo
) >> "$LOGPATH"
fi
else # different IP, execute update
curl --fail -sSL -n -A "$useragent" -o "$TEMP" "https://nic.changeip.com/nic/update?cmd=update&host=$CIPHOST"
if [ $LOGLEVEL -ne 0 ]; then # if logging, log update
(
echo "--------------------------------"
date
echo "Updating"
echo -n "NewIP: "
cat $TMPIP
if [ $LOGLEVEL -eq 2 ]
then # verbose logging
echo -n "OldIP: "
cat $IPPATH
cat $TEMP # log the ChangeIP.com update reply
echo
fi
) >> "$LOGPATH"
fi
cp $TMPIP $IPPATH # Store new IP
fi
# if $LOGMAX not equal to 0, reduce log size to last $LOGMAX number of lines
if [ $LOGMAX -ne 0 ]
then
tail -n $LOGMAX $LOGPATH > $TEMP
cp $TEMP $LOGPATH
fi
echo >> "$LOGPATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment