This commit is contained in:
parent
b1348e7133
commit
ae1afb952f
1 changed files with 80 additions and 63 deletions
59
dyndns.sh
59
dyndns.sh
|
@ -1,13 +1,14 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
api_host="https://api.digitalocean.com/v2"
|
api_host="https://api.digitalocean.com/v2"
|
||||||
sleep_interval=${SLEEP_INTERVAL:-300}
|
sleep_interval=${SLEEP_INTERVAL:-300}
|
||||||
remove_duplicates=${REMOVE_DUPLICATES:-"false"}
|
remove_duplicates=${REMOVE_DUPLICATES:-"false"}
|
||||||
|
|
||||||
|
# Only services with ipv6 supported are listed here.
|
||||||
services=(
|
services=(
|
||||||
"ifconfig.co"
|
"ifconfig.co"
|
||||||
"ipinfo.io/ip"
|
"ifconfig.io"
|
||||||
"ifconfig.me"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
|
@ -22,28 +23,16 @@ test -z $NAME && die "NAME not set!"
|
||||||
|
|
||||||
dns_list="$api_host/domains/$DOMAIN/records"
|
dns_list="$api_host/domains/$DOMAIN/records"
|
||||||
|
|
||||||
while ( true ); do
|
configure_record() {
|
||||||
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"
|
|
||||||
|
|
||||||
if [[ -n $ip ]]; then
|
|
||||||
# disable glob expansion
|
# disable glob expansion
|
||||||
set -f
|
set -f
|
||||||
|
|
||||||
|
ip=$1
|
||||||
|
type=$2
|
||||||
|
|
||||||
for sub in ${NAME//;/ }; do
|
for sub in ${NAME//;/ }; do
|
||||||
record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .id")
|
record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"$type\" and .name == \"$sub\") | .id")
|
||||||
record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data")
|
record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"$type\" and .name == \"$sub\") | .data")
|
||||||
|
|
||||||
if [ $(echo "$record_id" | wc -l) -ge 2 ]; then :
|
if [ $(echo "$record_id" | wc -l) -ge 2 ]; then :
|
||||||
if [[ "${remove_duplicates}" == "true" ]]; then :
|
if [[ "${remove_duplicates}" == "true" ]]; then :
|
||||||
|
@ -94,8 +83,36 @@ while ( true ); do
|
||||||
echo "existing DNS record address ($record_data) did not need updating"
|
echo "existing DNS record address ($record_data) did not need updating"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
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..."
|
||||||
|
|
||||||
|
ipv4="$(curl -4 -s -f $service)"
|
||||||
|
ipv6="$(curl -6 -s -f $service)"
|
||||||
|
|
||||||
|
test -n "$ipv4$ipv6" && break
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Found IPv4 address $ipv4"
|
||||||
|
echo "Found IPv6 address $ipv6"
|
||||||
|
|
||||||
|
if [[ -z $ipv4 ]]; then
|
||||||
|
echo "IPv4 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
||||||
else
|
else
|
||||||
echo "IP wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
configure_record $ipv4 "A"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z $ipv6 ]]; then
|
||||||
|
echo "IPv6 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
||||||
|
else
|
||||||
|
configure_record $ipv6 "AAAA"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep $sleep_interval
|
sleep $sleep_interval
|
||||||
|
|
Loading…
Reference in a new issue