From 8c940d5bd25c5c5616ee17b4214d22aea7f4359f Mon Sep 17 00:00:00 2001 From: Harald Ringvold Date: Sat, 20 Mar 2021 20:37:31 +0100 Subject: [PATCH 1/4] Create sub domain if it does not exist --- dyndns.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 941a69a..724c71d 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -45,13 +45,21 @@ while ( true ); do # re-enable glob expansion set +f + + data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" + url="$dns_list/$record_id" - test -z $record_id && echo "No record found with '$sub' domain name!" && continue + if [[ -z $record_id ]]; then + echo "No record found with '$sub' domain name. Creating record, sending data=$data to url=$url" + new_record=$(curl -s -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + -d "$data" \ + "$url") + record_data=$(echo $new_record| jq -r ".data") + fi if [[ "$ip" != "$record_data" ]]; then - data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" - url="$dns_list/$record_id" - echo "existing DNS record address ($record_data) doesn't match current IP ($ip), sending data=$data to url=$url" curl -s -X PUT \ From 9b59388a58bd79db6d43e169c0fb7903f6bff0d1 Mon Sep 17 00:00:00 2001 From: Harald Ringvold Date: Sat, 20 Mar 2021 21:09:58 +0100 Subject: [PATCH 2/4] Adding check for valid ip address In some cases the return body from ifconfig.co is an HTML error page. This change makes sure the response is a valid IP address. --- dyndns.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dyndns.sh b/dyndns.sh index 724c71d..f17a281 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -30,7 +30,8 @@ while ( true ); do echo "Trying with $service..." ip="$(curl -s $service)" - test -n "$ip" && break + valid_ip=$(echo $ip | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}') + test -n "$valid_ip" && break done echo "Found IP address $ip" From 928616e9e086a5e7a62f0d54561c641219cc6837 Mon Sep 17 00:00:00 2001 From: Harald Ringvold Date: Sat, 20 Mar 2021 21:14:24 +0100 Subject: [PATCH 3/4] Fix IP check for the case where all services fail Last commit would still not fix the problem if all services fails because of the check on line 38. --- dyndns.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index f17a281..325658a 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -29,9 +29,8 @@ while ( true ); do for service in ${services[@]}; do echo "Trying with $service..." - ip="$(curl -s $service)" - valid_ip=$(echo $ip | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}') - test -n "$valid_ip" && break + ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" + test -n "$ip" && break done echo "Found IP address $ip" From 0c4833645b1494e537a0430db79ffa8252de6c38 Mon Sep 17 00:00:00 2001 From: Alper Kanat Date: Wed, 24 Mar 2021 01:03:31 +0300 Subject: [PATCH 4/4] Update dyndns.sh --- dyndns.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dyndns.sh b/dyndns.sh index 325658a..f389b2d 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -51,11 +51,13 @@ while ( true ); do if [[ -z $record_id ]]; then echo "No record found with '$sub' domain name. Creating record, sending data=$data to url=$url" + new_record=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ -d "$data" \ "$url") + record_data=$(echo $new_record| jq -r ".data") fi