This commit is contained in:
Aaron Riedel 2022-04-25 17:26:32 +02:00
parent 3c60b1a02a
commit 92171b2ade
Signed by: aaron
GPG key ID: 643004654D40D577
3 changed files with 24 additions and 22 deletions

8
.env Normal file
View file

@ -0,0 +1,8 @@
##########################################################################################
# SETTINGS
HETZNER_API_TOKEN="" # https://dns.hetzner.com/settings/api-token
SUBDOMAIN_IPV4="" # desired subdomain e.g. "server1"
DNSZONE_IPV4="" # name of the zone in DNS Console e.g. "example.com"
SUBDOMAIN_IPV6="" # desired subdomain e.g. "server1"
DNSZONE_IPV6="" # name of the zone in DNS Console e.g. "example.com"
##########################################################################################

View file

@ -1,9 +1,6 @@
#!/bin/bash #!/bin/bash
########################################################################################## ##########################################################################################
# SETTINGS source .env
HETZNER_API_TOKEN="" # https://dns.hetzner.com/settings/api-token
SERVERNAME="" # desired subdomain e.g. "server1"
DNSZONE="" # name of the zone in DNS Console e.g. "example.com"
########################################################################################## ##########################################################################################
command_exists() { command_exists() {
command -v "$1" >/dev/null 2>&1 command -v "$1" >/dev/null 2>&1
@ -44,7 +41,7 @@ response_code() {
############################################# #############################################
echo -e "${INFO} DDNS Manager by Aaron" echo -e "${INFO} DDNS Manager by Aaron"
echo -e "${INFO} started $(date)" echo -e "${INFO} started $(date)"
echo -e "${INFO} DNS Record will be $SERVERNAME.$DNSZONE" echo -e "${INFO} DNS Record will be $SUBDOMAIN_IPV4.$DNSZONE_IPV4"
echo -e "${PENDING} get IP address" echo -e "${PENDING} get IP address"
IPv4="$(curl -s4 https://ip.hetzner.com)" IPv4="$(curl -s4 https://ip.hetzner.com)"
if [ -z $IPv4 ]; then if [ -z $IPv4 ]; then
@ -67,7 +64,7 @@ fi
############################################# #############################################
echo -e "${PENDING} get Zones" echo -e "${PENDING} get Zones"
HETZNER_API_ZONE=$(curl -s "https://dns.hetzner.com/api/v1/zones" -H "Auth-API-Token: ${HETZNER_API_TOKEN}" | jq -r ".zones[] | select(.name==\"$DNSZONE\") | .id") HETZNER_API_ZONE=$(curl -s "https://dns.hetzner.com/api/v1/zones" -H "Auth-API-Token: ${HETZNER_API_TOKEN}" | jq -r ".zones[] | select(.name==\"$DNSZONE_IPV4\") | .id")
if [ -z $HETZNER_API_ZONE ]; then if [ -z $HETZNER_API_ZONE ]; then
echo -e "${REPLACE}${FAIL} get DNS Zone" echo -e "${REPLACE}${FAIL} get DNS Zone"
exit 1 exit 1
@ -79,7 +76,7 @@ echo -e "${PENDING} Check DNS Console for existing records"
RECORDS=$(curl -s "https://dns.hetzner.com/api/v1/records?zone_id=${HETZNER_API_ZONE}" \ RECORDS=$(curl -s "https://dns.hetzner.com/api/v1/records?zone_id=${HETZNER_API_ZONE}" \
-H "Auth-API-Token: ${HETZNER_API_TOKEN}") -H "Auth-API-Token: ${HETZNER_API_TOKEN}")
echo -e "${REPLACE}${DONE} Check DNS Console for existing records" echo -e "${REPLACE}${DONE} Check DNS Console for existing records"
echo $RECORDS | jq -r '.records[] | select(.type=="A") | .name' | grep -q $SERVERNAME echo $RECORDS | jq -r '.records[] | select(.type=="A") | .name' | grep -q $SUBDOMAIN_IPV4
if [ $? -eq 1 ]; then if [ $? -eq 1 ]; then
echo -e "${INFO} Record not found" echo -e "${INFO} Record not found"
echo -e "${PENDING} Set new Record" echo -e "${PENDING} Set new Record"
@ -90,7 +87,7 @@ if [ $? -eq 1 ]; then
\"value\": \"${IPv4}\", \"value\": \"${IPv4}\",
\"ttl\": 60, \"ttl\": 60,
\"type\": \"A\", \"type\": \"A\",
\"name\": \"${SERVERNAME}\", \"name\": \"${SUBDOMAIN_IPV4}\",
\"zone_id\": \"${HETZNER_API_ZONE}\" \"zone_id\": \"${HETZNER_API_ZONE}\"
}") }")
if [ $API_STATUS_CODE != "200" ]; then if [ $API_STATUS_CODE != "200" ]; then
@ -101,8 +98,8 @@ if [ $? -eq 1 ]; then
fi fi
else else
echo -e "${INFO} Record already there" echo -e "${INFO} Record already there"
RECORD_ID=$(echo $RECORDS | jq -r '.records[] | select(.type=="A") | select(.name=="'${SERVERNAME}'") | .id' | head -1) RECORD_ID=$(echo $RECORDS | jq -r '.records[] | select(.type=="A") | select(.name=="'${SUBDOMAIN_IPV4}'") | .id' | head -1)
OLD_IP=$(echo $RECORDS | jq -r '.records[] | select(.type=="A") | select(.name=="'${SERVERNAME}'") | .value' | head -1) OLD_IP=$(echo $RECORDS | jq -r '.records[] | select(.type=="A") | select(.name=="'${SUBDOMAIN_IPV4}'") | .value' | head -1)
echo -e "${INFO} Current IP from Record: $OLD_IP" echo -e "${INFO} Current IP from Record: $OLD_IP"
if [ $IPv4 != $OLD_IP ]; then if [ $IPv4 != $OLD_IP ]; then
echo -e "${INFO} IP has changed" echo -e "${INFO} IP has changed"
@ -114,7 +111,7 @@ else
\"value\": \"${IPv4}\", \"value\": \"${IPv4}\",
\"ttl\": 60, \"ttl\": 60,
\"type\": \"A\", \"type\": \"A\",
\"name\": \"${SERVERNAME}\", \"name\": \"${SUBDOMAIN_IPV4}\",
\"zone_id\": \"${HETZNER_API_ZONE}\" \"zone_id\": \"${HETZNER_API_ZONE}\"
}") }")
if [ $API_STATUS_CODE != "200" ]; then if [ $API_STATUS_CODE != "200" ]; then

View file

@ -1,9 +1,6 @@
#!/bin/bash #!/bin/bash
########################################################################################## ##########################################################################################
# SETTINGS source .env
HETZNER_API_TOKEN="" # https://dns.hetzner.com/settings/api-token
SERVERNAME="" # desired subdomain e.g. "server1"
DNSZONE="" # name of the zone in DNS Console e.g. "example.com"
########################################################################################## ##########################################################################################
command_exists() { command_exists() {
command -v "$1" >/dev/null 2>&1 command -v "$1" >/dev/null 2>&1
@ -44,7 +41,7 @@ response_code() {
############################################# #############################################
echo -e "${INFO} DDNS Manager by Aaron" echo -e "${INFO} DDNS Manager by Aaron"
echo -e "${INFO} started $(date)" echo -e "${INFO} started $(date)"
echo -e "${INFO} DNS Record will be $SERVERNAME.$DNSZONE" echo -e "${INFO} DNS Record will be $SUBDOMAIN_IPV6.$DNSZONE_IPV6"
echo -e "${PENDING} get IP address" echo -e "${PENDING} get IP address"
IPv6="$(curl -s6 https://ip.hetzner.com)" IPv6="$(curl -s6 https://ip.hetzner.com)"
if [ -z $IPv6 ]; then if [ -z $IPv6 ]; then
@ -67,7 +64,7 @@ fi
############################################# #############################################
echo -e "${PENDING} get Zones" echo -e "${PENDING} get Zones"
HETZNER_API_ZONE=$(curl -s "https://dns.hetzner.com/api/v1/zones" -H "Auth-API-Token: ${HETZNER_API_TOKEN}" | jq -r ".zones[] | select(.name==\"$DNSZONE\") | .id") HETZNER_API_ZONE=$(curl -s "https://dns.hetzner.com/api/v1/zones" -H "Auth-API-Token: ${HETZNER_API_TOKEN}" | jq -r ".zones[] | select(.name==\"$DNSZONE_IPV6\") | .id")
if [ -z $HETZNER_API_ZONE ]; then if [ -z $HETZNER_API_ZONE ]; then
echo -e "${REPLACE}${FAIL} get DNS Zone" echo -e "${REPLACE}${FAIL} get DNS Zone"
exit 1 exit 1
@ -79,7 +76,7 @@ echo -e "${PENDING} Check DNS Console for existing records"
RECORDS=$(curl -s "https://dns.hetzner.com/api/v1/records?zone_id=${HETZNER_API_ZONE}" \ RECORDS=$(curl -s "https://dns.hetzner.com/api/v1/records?zone_id=${HETZNER_API_ZONE}" \
-H "Auth-API-Token: ${HETZNER_API_TOKEN}") -H "Auth-API-Token: ${HETZNER_API_TOKEN}")
echo -e "${REPLACE}${DONE} Check DNS Console for existing records" echo -e "${REPLACE}${DONE} Check DNS Console for existing records"
echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | .name' | grep -q $SERVERNAME echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | .name' | grep -q $SUBDOMAIN_IPV6
if [ $? -eq 1 ]; then if [ $? -eq 1 ]; then
echo -e "${INFO} Record not found" echo -e "${INFO} Record not found"
echo -e "${PENDING} Set new Record" echo -e "${PENDING} Set new Record"
@ -90,7 +87,7 @@ if [ $? -eq 1 ]; then
\"value\": \"${IPv6}\", \"value\": \"${IPv6}\",
\"ttl\": 60, \"ttl\": 60,
\"type\": \"AAAA\", \"type\": \"AAAA\",
\"name\": \"${SERVERNAME}\", \"name\": \"${SUBDOMAIN_IPV6}\",
\"zone_id\": \"${HETZNER_API_ZONE}\" \"zone_id\": \"${HETZNER_API_ZONE}\"
}") }")
if [ $API_STATUS_CODE != "200" ]; then if [ $API_STATUS_CODE != "200" ]; then
@ -101,8 +98,8 @@ if [ $? -eq 1 ]; then
fi fi
else else
echo -e "${INFO} Record already there" echo -e "${INFO} Record already there"
RECORD_ID=$(echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | select(.name=="'${SERVERNAME}'") | .id' | head -1) RECORD_ID=$(echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | select(.name=="'${SUBDOMAIN_IPV6}'") | .id' | head -1)
OLD_IP=$(echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | select(.name=="'${SERVERNAME}'") | .value' | head -1) OLD_IP=$(echo $RECORDS | jq -r '.records[] | select(.type=="AAAA") | select(.name=="'${SUBDOMAIN_IPV6}'") | .value' | head -1)
echo -e "${INFO} Current IP from Record: $OLD_IP" echo -e "${INFO} Current IP from Record: $OLD_IP"
if [ $IPv6 != $OLD_IP ]; then if [ $IPv6 != $OLD_IP ]; then
echo -e "${INFO} IP has changed" echo -e "${INFO} IP has changed"
@ -114,7 +111,7 @@ else
\"value\": \"${IPv6}\", \"value\": \"${IPv6}\",
\"ttl\": 60, \"ttl\": 60,
\"type\": \"AAAA\", \"type\": \"AAAA\",
\"name\": \"${SERVERNAME}\", \"name\": \"${SUBDOMAIN_IPV6}\",
\"zone_id\": \"${HETZNER_API_ZONE}\" \"zone_id\": \"${HETZNER_API_ZONE}\"
}") }")
if [ $API_STATUS_CODE != "200" ]; then if [ $API_STATUS_CODE != "200" ]; then