digitalocean-dyndns/dyndns.sh

103 lines
3.5 KiB
Bash
Raw Normal View History

2020-12-13 14:55:26 +00:00
#!/bin/bash
2016-01-23 16:47:35 +00:00
api_host="https://api.digitalocean.com/v2"
sleep_interval=${SLEEP_INTERVAL:-300}
2021-07-05 00:22:17 +00:00
remove_duplicates=${REMOVE_DUPLICATES:-"false"}
2016-01-23 16:47:35 +00:00
services=(
"ifconfig.co"
"ipinfo.io/ip"
"ifconfig.me"
)
2016-01-23 16:47:35 +00:00
die() {
echo "$1"
exit 1
}
test -f "$DIGITALOCEAN_TOKEN_FILE" && DIGITALOCEAN_TOKEN="$(cat $DIGITALOCEAN_TOKEN_FILE)"
2016-01-23 16:47:35 +00:00
test -z $DIGITALOCEAN_TOKEN && die "DIGITALOCEAN_TOKEN not set!"
test -z $DOMAIN && die "DOMAIN not set!"
test -z $NAME && die "NAME not set!"
dns_list="$api_host/domains/$DOMAIN/records"
while ( true ); do
domain_records=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
$dns_list"?per_page=200")
for service in ${services[@]}; do
echo "Trying with $service..."
ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')"
test -n "$ip" && break
done
echo "Found IP address $ip"
2016-01-23 16:47:35 +00:00
if [[ -n $ip ]]; then
2021-03-17 07:17:29 +00:00
# disable glob expansion
set -f
2020-01-30 13:38:37 +00:00
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 :
2021-07-05 00:22:17 +00:00
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
2021-07-05 00:28:13 +00:00
fi
fi
2021-03-17 07:17:29 +00:00
# re-enable glob expansion
set +f
2021-03-20 19:37:31 +00:00
data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}"
url="$dns_list/$record_id"
2021-03-20 19:37:31 +00:00
if [[ -z $record_id ]]; then
echo "No record found with '$sub' domain name. Creating record, sending data=$data to url=$url"
2021-03-23 22:03:31 +00:00
2021-03-20 19:37:31 +00:00
new_record=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-d "$data" \
"$url")
2021-03-23 22:03:31 +00:00
2021-03-20 19:37:31 +00:00
record_data=$(echo $new_record| jq -r ".data")
fi
2020-01-30 13:38:37 +00:00
if [[ "$ip" != "$record_data" ]]; then
echo "existing DNS record address ($record_data) doesn't match current IP ($ip), sending data=$data to url=$url"
curl -s -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-d "$data" \
"$url" &> /dev/null
else
2020-12-14 10:00:50 +00:00
echo "existing DNS record address ($record_data) did not need updating"
2020-01-30 13:38:37 +00:00
fi
done
2016-01-23 16:47:35 +00:00
else
echo "IP wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
fi
sleep $sleep_interval
done