1
0
mirror of https://github.com/kremalicious/dotfiles.git synced 2024-11-22 09:47:21 +01:00
dotfiles/bin/cloudflare-dyndns.sh

44 lines
1.5 KiB
Bash
Raw Normal View History

2021-02-16 20:24:14 +01:00
#!/usr/bin/env bash
2021-02-16 22:43:18 +01:00
#
# Update Cloudflare DNS record with current detected public IP
# https://api.cloudflare.com/#dns-records-for-a-zone-patch-dns-record
#
2021-02-16 20:24:14 +01:00
# Required env vars
# CLOUDFLARE_ZONE="xxx"
2021-02-16 21:18:04 +01:00
# CLOUDFLARE_DNS_RECORD_ID_A="xxx"
# CLOUDFLARE_DNS_RECORD_ID_AAAA="xxx"
2021-02-16 20:24:14 +01:00
# CLOUDFLARE_EMAIL="xxx"
# CLOUDFLARE_KEY="xxx"
2021-02-16 22:43:18 +01:00
function updateIpv4 {
# IPv4 A record
# https://unix.stackexchange.com/a/81699/125382
IPADDRv4=$(dig @resolver4.opendns.com myip.opendns.com +short)
echo "-----------------------------------------"
echo "Public IPv4: $IPADDRv4"
echo "-----------------------------------------"
2021-02-16 21:18:04 +01:00
2021-02-16 22:43:18 +01:00
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE/dns_records/$CLOUDFLARE_DNS_RECORD_ID_A" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_KEY" \
-H "Content-Type: application/json" \
2021-02-16 21:18:04 +01:00
--data "{\"content\":\"$IPADDRv4\"}"
2021-02-16 22:43:18 +01:00
}
2021-02-16 21:18:04 +01:00
2021-02-16 22:43:18 +01:00
function updateIpv6 {
2021-02-16 21:18:04 +01:00
# IPv6 AAAA record
IPADDRv6=$(dig @resolver1.ipv6-sandbox.opendns.com AAAA myip.opendns.com +short -6)
2021-02-16 22:43:18 +01:00
echo "-----------------------------------------"
2021-02-16 21:18:04 +01:00
echo "Public IPv6: $IPADDRv6"
2021-02-16 22:43:18 +01:00
echo "-----------------------------------------"
2021-02-16 21:18:04 +01:00
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE/dns_records/$CLOUDFLARE_DNS_RECORD_ID_AAAA" \
2021-02-16 22:43:18 +01:00
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_KEY" \
-H "Content-Type: application/json" \
--data "{\"content\":\"$IPADDRv6\"}"
}
2021-02-16 20:24:14 +01:00
2021-02-16 22:43:18 +01:00
# Let's roll
updateIpv4
updateIpv6