Created
January 29, 2022 06:12
-
-
Save masato9000/f78da485679c57eb2c76f95ea083b5c0 to your computer and use it in GitHub Desktop.
DuckDNS updater for (open)BSD
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
#!/bin/sh | |
# | |
# duckdns.sh -d <domain[,...,domain]> -t <token> [clear|noverify] [force] | |
# noverify - Don't fail if public inet address can't be bound to the router | |
# because of modem+gateway combos forced on you by shitty ISPs. | |
# CAUTION: It's best if your NAT is 1:1 or you at least have some control | |
# over port forwarding, or DDNS isn't going to be as useful... | |
# force - Always update, even if the logs indicate no change is necessary | |
# clear - Not allowed with or "noverify" | |
# | |
_bsdl=' | |
Copyright (c) 2021, masato9000@users.noreply.github.com | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
' | |
_DUCKDIR="/var/db/duckdns" | |
_DUCKLOG="duckdns.log" | |
_CFDNS4=1.1.1.1 | |
_CFDNS6=2606:4700:4700::1111 | |
unset _DOCLEAR _NOVERIFY _DOFORCE _DOV4 _DOV6 _INETPUB _INETPUB6 | |
#### Process args | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-d) | |
_DOMAINS=$2 | |
shift 2 | |
;; | |
-t) | |
_TOKEN=$2 | |
shift 2 | |
;; | |
clear) | |
_DOCLEAR=1 | |
shift | |
;; | |
noverify) | |
_NOVERIFY=1 | |
shift | |
;; | |
force) | |
_DOFORCE="FORCED " | |
_DOV4=1 | |
_DOV6=1 | |
shift | |
;; | |
*) | |
echo Invalid arguments. PLEASE READ ME. | |
exit 1 | |
;; | |
esac | |
if [ -n "$_DOCLEAR" ] && [ -n "$_NOVERIFY" ]; then | |
echo Invalid argument combination. PLEASE READ ME. | |
exit 1 | |
fi | |
done | |
if [ -z "$_DOMAINS" ] || [ -z "$_TOKEN" ]; then | |
echo both domain list and API token must be specified | |
exit 1 | |
fi | |
_DUCKURL="https://www.duckdns.org/update?domains=${_DOMAINS}&token=${_TOKEN}&verbose=true" | |
do_clear() { | |
echo clearing records for $_DOMAINS | sed 's/,/, /g' | |
_response=$(curl --connect-timeout 15 "${_DUCKURL}&clear=true") | |
case "${_response}X" in | |
X) | |
echo No Response Received | |
exit 1 | |
;; | |
KO*) | |
echo Server Responded with | |
echo "$_response" | |
exit 1 | |
;; | |
OK*) | |
echo "$_response" | |
;; | |
*) | |
echo Unknown Error | |
exit 1 | |
;; | |
esac | |
} | |
do_update() { | |
echo updating records for $_DOMAINS | sed 's/,/, /g' | |
unset _upd4 _upd6 | |
[ -n "$_DOV4" ] || [ -n "$_DOCLEAR" ] && _upd4="&ip=$_INETPUB" | |
[ -n "$_DOV6" ] && [ -n "$_INETPUB6" ] && _upd6="&ipv6=$_INETPUB6" | |
_response=$(curl --connect-timeout 15 \ | |
"${_DUCKURL}${_upd4}${_upd6}") | |
case "${_response}X" in | |
X) | |
echo No Response Received | |
exit 1 | |
;; | |
KO*) | |
echo Server Responded with | |
echo "$_response" | |
exit 1 | |
;; | |
OK*) | |
echo "$_response" | |
;; | |
*) | |
echo Unknown Error | |
exit 1 | |
;; | |
esac | |
} | |
do_log() { | |
echo [${_DOFORCE}UPDATE: $_TODAY] >> "${_DUCKDIR}/${_DUCKLOG}" | |
[ -n "$_DOV4" ] && echo "inet: $_INETPUB" >> "${_DUCKDIR}/${_DUCKLOG}" | |
[ -n "$_DOV6" ] && echo "inet6: $_INETPUB6" >> "${_DUCKDIR}/${_DUCKLOG}" | |
} | |
_TODAY="$(date +%Y.%m.%d\ %H:%M:%S)" | |
#### Verify or set up history | |
[ -d "$_DUCKDIR" ] && [ -w "$_DUCKDIR" ] || mkdir -p "$_DUCKDIR" || exit 1 | |
[ -f "${_DUCKDIR}/${_DUCKLOG}" ] && [ -w "${_DUCKDIR}/${_DUCKLOG}" ] || touch "${_DUCKDIR}/${_DUCKLOG}" || exit 1 | |
#### Import addresses from history | |
_LASTV4=$(grep '^inet:' "${_DUCKDIR}/${_DUCKLOG}" | tail -n 1) | |
_LASTV4=${_LASTV4##*[[:space:]]} | |
_LASTV6=$(grep '^inet6:' "${_DUCKDIR}/${_DUCKLOG}" | tail -n 1) | |
_LASTV6=${_LASTV6##*[[:space:]]} | |
#### Clear can be attempted at this point | |
if [ -n "$_DOCLEAR" ]; then | |
if [ -n "$_LASTV4" ] || [ -n "$_LASTV6" ] || [ -n "$_DOFORCE" ]; then | |
do_clear | |
[ -n "$_LASTV4" ] && _DOV4=1 | |
[ -n "$_LASTV6" ] && _DOV6=1 | |
do_log | |
fi | |
exit | |
fi | |
#### Detect interface with default ip route | |
_EXTIF=$(netstat -f inet -rn | awk '/^default/ {print $NF}') | |
if [ -z "$_EXTIF" ]; then | |
echo No IP default route. Updates only work over IP. Aborting. | |
exit 1 | |
fi | |
#### Detect our real IP addresses (thanks CloudFlare!) | |
_INETPUB=$(dig +short txt ch whoami.cloudflare @$_CFDNS4 | tr -d '"') | |
if [ ${_INETPUB} != ${_INETPUB#;;} ] ; then | |
echo Cannot determine public IP. Either we\'re down or CF is down. | |
exit 1 | |
fi | |
_INETPUB6=$(dig +short txt ch whoami.cloudflare @$_CFDNS6 | tr -d '"') | |
#### Determine which updates should be made | |
[ "${_INETPUB}X" = "${_LASTV4}X" ] || _DOV4=1 | |
[ "${_INETPUB6}X" = "${_LASTV6}X" ] || _DOV6=1 | |
#### Verify addresses are bound to external NIC | |
if ! ifconfig $_EXTIF | grep $_INETPUB; then | |
echo Public IP is not bound to THIS system's outbound interface | |
echo Looks like we\'re NATed. | |
if [ -n "$_NOVERIFY" ]; then | |
echo noverify OK. Will update DNS with real public IP. | |
else | |
echo If you\'re aware and this is OK, use the noverify flag | |
exit 1 | |
fi | |
fi | |
if [ -n "$_INETPUB6" ] && ! ifconfig $_EXTIF | grep $_INETPUB6; then | |
echo Public IPv6 is not bound to THIS system\'s outbound interface | |
echo Something nonstandard is happening. Not touching ipv6. | |
unset _DOV6 | |
fi | |
# If we lose our inet6 and have to clear, must clear both and then update | |
if [ -n "$_DOV6" ] && [ -z "$_INETPUB6" ]; then | |
_DOCLEAR=1 | |
do_clear | |
fi | |
if [ -n "$_DOV4" ] || [ -n "$_DOV6" ] || [ -n "$_DOCLEAR" ]; then | |
do_update | |
do_log | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While this could be done by the simple example on the duckdns spec page, I felt like doing a bit more...
I have it running every 3 minutes in my router's crontab