Skip to content

Instantly share code, notes, and snippets.

@davidderus
Last active July 21, 2016 15:52
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 davidderus/939d82bb62265828cdd4636d67331b6c to your computer and use it in GitHub Desktop.
Save davidderus/939d82bb62265828cdd4636d67331b6c to your computer and use it in GitHub Desktop.
Updates a DtDNS Hostname with the current device public IP
#!/bin/bash
# Updates a DtDNS Hostname with the current device public IP.
# You may run this script via an hourly CRON (DtDNS propagation is around 15min so a shorter rerun time is useless).
#
# Author: David Dérus
# Version: 1.1.3
# Config
HOSTNAME='MYHOSTNAME'
PASSWORD='MYPASSWORD'
# Gets the IP returned by an host request
# Usage: getIPByHost HOST RESOLVER
getIPByHost() {
host -t A $1 $2 | tail -n 1 | cut -d ' ' -f4
}
# Validates an IP
# Usage: validateIP IP
validateIP() {
if ! [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
echo "Invalid IP '$1'"
exit 1
fi
}
# Updates a DtDNS Hostname IP
# Usage: updateDnsEntry HOSTNAME PASSWORD NEW_IP
updateDnsEntry() {
wget -qO- --user-agent="DtDNSUpdater/1.1.3" --header="Host:www.dtdns.com" "https://www.dtdns.com/api/autodns.cfm?id=$1&pw=$2&client=DtDNSUpdater_1.1.3&ip=$3" &> /dev/null
}
# Getting IPs
CURRENT_IP=$(getIPByHost myip.opendns.com resolver1.opendns.com)
LAST_KNOWN_IP=$(getIPByHost $HOSTNAME ns1.dtdns.com)
# Validating both IPs
validateIP $CURRENT_IP && validateIP $LAST_KNOWN_IP
# Updating if needed
if [ $CURRENT_IP != $LAST_KNOWN_IP ] ; then
updateDnsEntry $HOSTNAME $PASSWORD $CURRENT_IP
echo "IP changed from $LAST_KNOWN_IP to $CURRENT_IP"
fi
exit 0
@davidderus
Copy link
Author

Reminder: Even if OpenDNS resolver is safer than ifconfig.me and others, the best solution is to get your public IP from your router (if it's possible of course)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment