#!/bin/bash
# Hetzner Access Token
TOKEN=""
# Abfrage der IDs mit curl "https://dns.hetzner.com/api/v1/records?zone_id=[ZONEID]" -H 'Auth-API-Token: [TOKEN]'
# ID der Dyndns Zone
ZONEID=""
#####################################################
WHICH=$(/usr/bin/which which)
DIG=$($WHICH dig)
IP=$($WHICH ip)
GREP=$($WHICH grep)
AWK=$($WHICH awk)
CURL=$($WHICH curl)
CUT=$($WHICH cut)
JQ=$($WHICH jq)
#####################################################
MYOLDPREFIX=$($DIG @194.25.2.129 +short [TESTDOMAIN] AAAA | $CUT -d: -f1,2,3,4)
MYOLDV4=$($DIG +short [TESTDOMAIN] A)
IPV6=$($IP -6 addr | $GREP inet6 | $GREP -v deprecated | $AWK -F '[ \t]+|/' '{print $3}' | $GREP -v ^::1 | $GREP -v ^fe80 | $GREP -v ^fd)
IPV4=$($CURL -4 -s https://stoltz-it.de/tools/ip_console)
PREFIX=$($IP -6 addr | $GREP inet6 | $GREP -v deprecated | $AWK -F '[ \t]+|/' '{print $3}' | $GREP -v ^::1 | $GREP -v ^fe80 | $GREP -v ^fd | $CUT -d: -f1,2,3,4)
#####################################################
ARRAY_ID=("[RECORDID]" "weitere")
ARRAY_DOMAIN=("[DOMAIN]" "weitere")
ARRAY_IP=("$IPV4" "$IPV6" "$PREFIX:abcd:1234:0000:dcba")
ARRAY_TYPE=("A" "AAAA" "AAAA")
if [ $PREFIX != $MYOLDPREFIX ] || [ $IPV4 != $MYOLDV4 ]; then
for i in ${!ARRAY_ID[@]}; do
$CURL -s -X "PUT" "https://dns.hetzner.com/api/v1/records/${ARRAY_ID[$i]}" \
-H "Content-Type: application/json" \
-H "Auth-API-Token: $TOKEN" \
-d $'{
"value":"'"${ARRAY_IP[$i]}"'",
"ttl": 60,
"type": "'"${ARRAY_TYPE[$i]}"'",
"name": "'"${ARRAY_DOMAIN[$i]}"'",
"zone_id":"'"$ZONEID"'"
}' | $JQ -r '.error.message'
done
fi