2016-01-23 16:47:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
api_host="https://api.digitalocean.com/v2"
|
|
|
|
sleep_interval=${SLEEP_INTERVAL:-300}
|
|
|
|
|
2020-12-10 21:03:27 +00:00
|
|
|
services=(
|
|
|
|
"ipinfo.io/ip"
|
|
|
|
"ifconfig.me"
|
|
|
|
)
|
|
|
|
|
2016-01-23 16:47:35 +00:00
|
|
|
die() {
|
|
|
|
echo "$1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-12-01 02:34:38 +00:00
|
|
|
domain_records=$(curl -s -X GET \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
2020-06-25 18:51:17 +00:00
|
|
|
$dns_list"?per_page=200")
|
2017-12-01 02:34:38 +00:00
|
|
|
|
2020-12-10 21:03:27 +00:00
|
|
|
for service in ${services[@]}; do
|
|
|
|
echo "Trying with $service..."
|
|
|
|
|
|
|
|
ip="$(curl -s $service)"
|
|
|
|
|
|
|
|
test -n "$ip" && break
|
|
|
|
done
|
2016-01-23 16:47:35 +00:00
|
|
|
|
|
|
|
if [[ -n $ip ]]; then
|
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")
|
|
|
|
|
|
|
|
test -z $record_id && echo "No record found with '$sub' domain name!" && continue
|
|
|
|
|
|
|
|
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 \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
|
|
|
-d "$data" \
|
|
|
|
"$url" &> /dev/null
|
|
|
|
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
|