Last active
January 3, 2017 22:13
-
-
Save ffflorian/fed6f7777189b50cdf7d4ea087a81b83 to your computer and use it in GitHub Desktop.
dynv6.net updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Usage: dynv6.sh --token TOKEN --name NAME1,NAME2 | |
# Created by @ffflorian (https://github.com/ffflorian) | |
# Licensed under the MIT license. | |
SCRIPT_NAME="${0##*/}" | |
_print_usage() { | |
cat <<EOF | |
Usage: ${SCRIPT_NAME} [-h] [switch] [command] <NAME[,NAME2, ...]> | |
Commands: | |
--help (-h) Show help text | |
--name (-n) example.com Set the update URL | |
[,example2.com,...] | |
Switches: | |
--token (-t) 3d3192b72... Set the update token | |
--quiet (-q) Display only errors | |
EOF | |
} | |
_command_exist() { | |
command -v "${1}" > /dev/null | |
} | |
_log() { | |
echo "${2}" "[$(date +'%Y-%m-%d %H:%M:%S')] ${1}" | |
} | |
while : | |
do | |
case "$1" in | |
-t|--token ) | |
TOKEN="${2}" | |
shift 2 | |
;; | |
-n|--name ) | |
NAMES="${2}" | |
shift 2 | |
;; | |
-h|--help ) | |
_print_usage | |
exit 0 | |
;; | |
-q|--quiet ) | |
QUIET="quiet" | |
shift 1 | |
;; | |
* ) | |
break | |
;; | |
esac | |
done | |
if [[ -z "${TOKEN}" || -z "${NAMES}" ]]; then | |
_print_usage | |
exit 1 | |
fi | |
if ! _command_exist dig; then | |
_log "dig not found. Please install dnsutils." | |
exit 1 | |
fi | |
PUBLIC_IP="$(wget http://ipecho.net/plain -O - -q)" | |
if _command_exist curl; then | |
BIN="curl -fsS" | |
elif _command_exist wget; then | |
BIN="wget -qO-" | |
else | |
_log "Neither wget nor curl found. Can not update." | |
exit 1 | |
fi | |
for NAME in ${NAMES//,/ } | |
do | |
if grep -q '^[a-z0-9-]\{5,20\}$' <<< "${NAME}"; then | |
NAME="${NAME}.dynv6.net" | |
else | |
if ! grep -q '^[a-z0-9-]\{5,20\}.dynv6.net$' <<< "${NAME}"; then | |
_log "${NAME}: Invalid hostname!" | |
exit 1 | |
fi | |
fi | |
IPFILE="${HOME}/.${NAME}.addr4" | |
if [ -r "${IPFILE}" ] && [ "${PUBLIC_IP}" = "$(cat "${IPFILE}")" ]; then | |
[ "${QUIET}" != "quiet" ] && _log "${NAME} is current." | |
continue | |
fi | |
[ "${QUIET}" != "quiet" ] && _log "Updating ${NAME} ... " "-n" | |
UPDATE_URL="http://ipv4.dynv6.com/api/update?hostname=${NAME}&ipv4=${PUBLIC_IP}&token=${TOKEN}" | |
if [ "${QUIET}" = "quiet" ]; then | |
${BIN} "${UPDATE_URL}" > /dev/null && UPDATED="updated" | |
else | |
${BIN} "${UPDATE_URL}" && UPDATED="updated" | |
fi | |
if [ "${UPDATED}" = "updated" ]; then | |
echo "${PUBLIC_IP}" > "${IPFILE}" | |
else | |
_log "Update failed!" | |
exit 1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment