hetzner-ddns/ddns4.sh

128 lines
4.7 KiB
Bash
Raw Normal View History

2022-03-07 06:45:46 +01:00
#!/bin/bash
##########################################################################################
source $(dirname "$0")/.env
2022-03-07 06:45:46 +01:00
##########################################################################################
command_exists() {
command -v "$1" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Software dependency not met: $1"
exit 1
fi
}
for COMMAND in "curl" "jq" "grep"; do
command_exists "${COMMAND}"
done
#############################################
2022-04-21 18:34:50 +02:00
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
REPLACE='\e[1A\e[K'
PENDING="${NONE}[${YELLOW}....${NONE}]"
2022-04-25 16:32:36 +02:00
DONE="${NONE}[${GREEN} OK ${NONE}]"
FAIL="${NONE}[${RED}FAIL${NONE}]"
INFO="${NONE}[${BOLD}INFO${NONE}]"
2022-04-21 18:34:50 +02:00
#############################################
2022-03-07 06:45:46 +01:00
#HTTP Response Code Echo
response_code() {
2023-03-22 21:36:24 +01:00
if [[ $1 == "200" ]]; then echo "$1 Success";
elif [[ $1 == "401" ]]; then echo "$1 Unauthorized" && exit 1;
elif [[ $1 == "403" ]]; then echo "$1 Forbidden" && exit 1;
elif [[ $1 == "406" ]]; then echo "$1 Not acceptable" && exit 1;
elif [[ $1 == "422" ]]; then echo "$1 Unprocessable entity" && exit 1;
2022-03-07 06:45:46 +01:00
else echo "$1 Unknown Status Code" && exit 1; fi
}
#############################################
2022-04-21 18:34:50 +02:00
echo -e "${INFO} DDNS Manager by Aaron"
echo -e "${INFO} started $(date)"
2022-04-25 17:26:32 +02:00
echo -e "${INFO} DNS Record will be $SUBDOMAIN_IPV4.$DNSZONE_IPV4"
2022-04-21 18:34:50 +02:00
echo -e "${PENDING} get IP address"
2022-03-07 06:45:46 +01:00
IPv4="$(curl -s4 https://ip.hetzner.com)"
2023-03-22 21:36:24 +01:00
if [[ -z $IPv4 ]]; then
echo -e "${REPLACE}${FAIL} IPv4 not found"
2022-04-21 18:34:50 +02:00
exit 1
else
echo -e "${REPLACE}${DONE} get IP address"
2022-04-21 18:34:50 +02:00
echo -e "${INFO} IP address is $IPv4"
fi
2022-03-07 06:45:46 +01:00
#############################################
2022-04-21 18:34:50 +02:00
echo -e "${PENDING} attempt API connection"
2022-03-07 06:45:46 +01:00
API_STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}" "https://dns.hetzner.com/api/v1/zones" -H "Auth-API-Token: ${HETZNER_API_TOKEN}")
2023-03-22 21:36:24 +01:00
if [[ $API_STATUS_CODE != "200" ]]; then
echo -e "${REPLACE}${FAIL} attempt API connection ($(response_code $API_STATUS_CODE))"
2022-03-07 06:45:46 +01:00
exit 1
2022-04-21 18:34:50 +02:00
else
echo -e "${REPLACE}${DONE} attempt API connection ($(response_code $API_STATUS_CODE))"
2022-03-07 06:45:46 +01:00
fi
2022-04-21 18:34:50 +02:00
2022-03-07 06:45:46 +01:00
#############################################
2022-04-21 18:34:50 +02:00
echo -e "${PENDING} get Zones"
2022-04-25 17:26:32 +02:00
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")
2023-03-22 21:36:24 +01:00
if [[ -z $HETZNER_API_ZONE ]]; then
echo -e "${REPLACE}${FAIL} get DNS Zone"
exit 1
else
echo -e "${REPLACE}${DONE} get DNS Zone"
fi
2022-03-07 06:45:46 +01:00
#############################################
2022-04-21 18:34:50 +02:00
echo -e "${PENDING} Check DNS Console for existing records"
2022-03-07 06:45:46 +01:00
RECORDS=$(curl -s "https://dns.hetzner.com/api/v1/records?zone_id=${HETZNER_API_ZONE}" \
-H "Auth-API-Token: ${HETZNER_API_TOKEN}")
echo -e "${REPLACE}${DONE} Check DNS Console for existing records"
2023-03-22 21:36:24 +01:00
echo $RECORDS | jq -r '.records[] | select(.type=="A") | .name' | grep -qx $SUBDOMAIN_IPV4
if [[ $? -eq 1 ]]; then
echo -e "${INFO} Record not found"
2022-04-21 18:34:50 +02:00
echo -e "${PENDING} Set new Record"
API_STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}" -X "POST" "https://dns.hetzner.com/api/v1/records" \
2022-03-07 06:45:46 +01:00
-H 'Content-Type: application/json' \
-H "Auth-API-Token: ${HETZNER_API_TOKEN}" \
-d $"{
\"value\": \"${IPv4}\",
\"ttl\": 60,
\"type\": \"A\",
2022-04-25 17:26:32 +02:00
\"name\": \"${SUBDOMAIN_IPV4}\",
2022-03-07 06:45:46 +01:00
\"zone_id\": \"${HETZNER_API_ZONE}\"
}")
2023-03-22 21:36:24 +01:00
if [[ $API_STATUS_CODE != "200" ]]; then
2022-04-26 00:43:26 +02:00
echo -e "${REPLACE}${FAIL} Set new Record ($(response_code $API_STATUS_CODE))"
2022-04-21 18:34:50 +02:00
exit 1
else
2022-04-26 00:43:26 +02:00
echo -e "${REPLACE}${DONE} Set new Record ($(response_code $API_STATUS_CODE))"
2022-04-21 18:34:50 +02:00
fi
2022-03-07 06:45:46 +01:00
else
2022-04-21 18:34:50 +02:00
echo -e "${INFO} Record already there"
2022-04-25 17:26:32 +02:00
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=="'${SUBDOMAIN_IPV4}'") | .value' | head -1)
2022-04-21 18:34:50 +02:00
echo -e "${INFO} Current IP from Record: $OLD_IP"
2023-03-22 21:36:24 +01:00
if [[ $IPv4 != $OLD_IP ]]; then
2022-04-21 18:34:50 +02:00
echo -e "${INFO} IP has changed"
echo -e "${PENDING} Updating Record"
API_STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}" -X "PUT" "https://dns.hetzner.com/api/v1/records/$RECORD_ID" \
2022-03-07 06:45:46 +01:00
-H 'Content-Type: application/json' \
-H "Auth-API-Token: ${HETZNER_API_TOKEN}" \
-d $"{
\"value\": \"${IPv4}\",
\"ttl\": 60,
\"type\": \"A\",
2022-04-25 17:26:32 +02:00
\"name\": \"${SUBDOMAIN_IPV4}\",
2022-03-07 06:45:46 +01:00
\"zone_id\": \"${HETZNER_API_ZONE}\"
}")
2023-03-22 21:36:24 +01:00
if [[ $API_STATUS_CODE != "200" ]]; then
2022-04-26 00:43:26 +02:00
echo -e "${REPLACE}${FAIL} Updating Record ($(response_code $API_STATUS_CODE))"
2022-04-21 18:34:50 +02:00
exit 1
else
2022-04-26 00:43:26 +02:00
echo -e "${REPLACE}${DONE} Updating Record ($(response_code $API_STATUS_CODE))"
2022-04-21 18:34:50 +02:00
fi
2022-03-07 06:45:46 +01:00
else
2022-04-21 18:34:50 +02:00
echo -e "${INFO} IP has not changed"
2022-03-07 06:45:46 +01:00
fi
fi
exit 0