From 89fd57cfe8132b2c5bd10a0a5532a634b7357e1e Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Wed, 16 Jun 2021 20:59:05 -0700 Subject: [PATCH 1/5] Add duplicate DNS record handling --- dyndns.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 561b029..e446078 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -33,20 +33,35 @@ while ( true ); do ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" test -n "$ip" && break done - + echo "Found IP address $ip" if [[ -n $ip ]]; then # disable glob expansion set -f - + for sub in ${NAME//;/ }; do record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .id") record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") - + + if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +1) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) + echo "record_id_to_delete = $record_id_to_delete record_id = $record_id record_data = $record_data" + + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + fi + # re-enable glob expansion set +f - + data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" url="$dns_list/$record_id" From 59c288723691934ca73062f06eb1eef4ca5ba2db Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Wed, 16 Jun 2021 21:05:34 -0700 Subject: [PATCH 2/5] Fix tail command and remove unnecessary echo --- dyndns.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 561b029..acf6409 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -33,20 +33,34 @@ while ( true ); do ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" test -n "$ip" && break done - + echo "Found IP address $ip" if [[ -n $ip ]]; then # disable glob expansion set -f - + for sub in ${NAME//;/ }; do record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .id") record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") - + + if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +2) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) + + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + fi + # re-enable glob expansion set +f - + data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" url="$dns_list/$record_id" From 3c754751b17cffedb739307dd01565934254f069 Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Sun, 4 Jul 2021 17:22:17 -0700 Subject: [PATCH 3/5] Add REMOVE_DUPLICATES env variable --- README.md | 2 ++ dyndns.sh | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 37670a9..07ee4bf 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Pick one of the options below using the following settings: * **DOMAIN:** The domain your subdomain is registered at. (i.e. `foo.com` for `home.foo.com`) * **NAME:** Subdomain to use. (name in A record) (i.e. `home` for `home.foo.com`). Multiple subdomains must be separated by semicolons `;` * **SLEEP_INTERVAL:** Polling time in seconds. (default: 300) +* **REMOVE_DUPLICATES:** If set to `"true"`, removes extra DNS records if more than one A record is found on a subdomain. *Note that if this is not enabled, the script will NOT update subdomains with more than one A record* (default: false) ### Docker (Recommended) @@ -26,6 +27,7 @@ $ docker run -d --name dyndns \ -e DOMAIN="yourdomain.com" \ -e NAME="subdomain" \ -e SLEEP_INTERVAL=2 \ + -e REMOVE_DUPLICATES="true" \ tunix/digitalocean-dyndns ``` diff --git a/dyndns.sh b/dyndns.sh index acf6409..cead896 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -2,6 +2,7 @@ api_host="https://api.digitalocean.com/v2" sleep_interval=${SLEEP_INTERVAL:-300} +remove_duplicates=${REMOVE_DUPLICATES:-"false"} services=( "ifconfig.co" @@ -45,17 +46,21 @@ while ( true ); do record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : - echo "'$sub' domain name has duplicate DNS records, removing duplicates" - record_id_to_delete=$(echo "$record_id"| tail -n +2) - record_id=$(echo "$record_id"| head -1) - record_data=$(echo "$record_data"| head -1) + if [[ "${remove_duplicates}" == "true" ]]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +2) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) - while IFS= read -r line; do - curl -s -X DELETE \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ - "$dns_list/$line" &> /dev/null - done <<< "$record_id_to_delete" + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + else : + echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." + continue fi # re-enable glob expansion From 8c6d54ac9e8d6defd2c76e2e30477a09ec7914e9 Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Sun, 4 Jul 2021 17:28:13 -0700 Subject: [PATCH 4/5] Add missing fi --- dyndns.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/dyndns.sh b/dyndns.sh index cead896..8745acd 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -61,6 +61,7 @@ while ( true ); do else : echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." continue + fi fi # re-enable glob expansion From 85604371339deeb426fbc042d3195eea1c7bef68 Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Sun, 4 Jul 2021 17:30:45 -0700 Subject: [PATCH 5/5] Fix indentation --- dyndns.sh | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 8745acd..0d9709c 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -46,22 +46,22 @@ while ( true ); do record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : - if [[ "${remove_duplicates}" == "true" ]]; then : - echo "'$sub' domain name has duplicate DNS records, removing duplicates" - record_id_to_delete=$(echo "$record_id"| tail -n +2) - record_id=$(echo "$record_id"| head -1) - record_data=$(echo "$record_data"| head -1) + if [[ "${remove_duplicates}" == "true" ]]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +2) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) - while IFS= read -r line; do - curl -s -X DELETE \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ - "$dns_list/$line" &> /dev/null - done <<< "$record_id_to_delete" - else : - echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." - continue - fi + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + else : + echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." + continue + fi fi # re-enable glob expansion