From d03bf1f30cfb3344ea018e9ae37731cbc6b7bffe Mon Sep 17 00:00:00 2001 From: Logan G Date: Sat, 16 Nov 2024 18:07:58 -0700 Subject: [PATCH] Added generic Njalla DDNS script --- misc/generic-njalla-ddns.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 misc/generic-njalla-ddns.sh diff --git a/misc/generic-njalla-ddns.sh b/misc/generic-njalla-ddns.sh new file mode 100644 index 0000000..bd94fbf --- /dev/null +++ b/misc/generic-njalla-ddns.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +IP_TEMPFILE=/tmp/wanip +NJALLA_SUBDOMAIN="thing.com" +NJALLA_KEY="1234" + +# Gets the current WAN interface and IP(s) +NET_ADDR=$(ip route get 9.9.9.9 | grep src | sed 's/.*src \(.* \)/\1/g' | cut -f1 -d ' ') +NET_ADDR6=$(ip route get 2620:fe::fe | grep src | sed 's/.*src \(.* \)/\1/g' | cut -f1 -d ' ') + +if [ "$1" = "--query" ] || [ ! -f ${IP_TEMPFILE} ]; then + echo "Querying IP from DNS server" + NET_ADDR_OLD="$(nslookup -type=a ${NJALLA_SUBDOMAIN} | awk '/^Address: / { print $2 }')" + NET_ADDR6_OLD="$(nslookup -type=aaaa ${NJALLA_SUBDOMAIN} | awk '/^Address: / { print $2 }')" + + if [ ! -f ${IP_TEMPFILE} ]; then + touch ${IP_TEMPFILE} || exit 1 + echo "NET_ADDR_OLD=${NET_ADDR_OLD}" > $IP_TEMPFILE + echo "NET_ADDR6_OLD=${NET_ADDR6_OLD}" >> $IP_TEMPFILE + fi +else + . ${IP_TEMPFILE} +fi + +if [ "$NET_ADDR" != "$NET_ADDR_OLD" ] || [ "$NET_ADDR6" != "$NET_ADDR6_OLD" ] || [ "$1" = "--force" ]; then + echo "IPv4: ${NET_ADDR_OLD} => ${NET_ADDR}" + echo "IPv6: ${NET_ADDR6_OLD} => ${NET_ADDR6}" + + wget -q -O /dev/null "https://njal.la/update/?h=${NJALLA_SUBDOMAIN}&k=${NJALLA_KEY}&a=${NET_ADDR}&aaaa=${NET_ADDR6}" + STATUS=$? + if [ $STATUS -ne 0 ]; then + echo "Could not set DDNS IP! (${STATUS})" + fi + + echo "NET_ADDR_OLD=${NET_ADDR}" > $IP_TEMPFILE + echo "NET_ADDR6_OLD=${NET_ADDR6}" >> $IP_TEMPFILE +fi