Last active
July 30, 2021 21:53
-
-
Save OnkelDom/c457caa059b465f718d18008962cf701 to your computer and use it in GitHub Desktop.
Update A und AAAA record for dnsrecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A bash script to update a Cloudflare DNS A and AAAA record with the external IP of the source machine | |
# Used to provide DDNS service for my home | |
# Needs the DNS record pre-creating on Cloudflare | |
# I run this directly on my router (usg) as cronjob every 15 minutes | |
# put this script in root or another folder | |
# put the following line in you cron.d folder | |
# "*/15 * * * * root /root/cloudflare-ddns.sh 2>&1 | logger -t cloudflare" | |
# Proxy - uncomment and provide details if using a proxy | |
#export https_proxy=http://<proxyuser>:<proxypassword>@<proxyip>:<proxyport> | |
# Cloudflare zone is the zone which holds the record | |
zone=exmaple.com | |
# dnsrecord is the A record which will be updated | |
dnsrecord=home.exmaple.com | |
## Cloudflare authentication details | |
## keep these private | |
cloudflare_auth_email=<email> | |
cloudflare_auth_key=<key> | |
# Get the current external IP address | |
ipv4=$(curl -4s -X GET https://ifconfig.io) | |
ipv6=$(curl -6s -X GET https://ifconfig.io) | |
echo "Current IPv4 is $ipv4" | |
echo "Current IPv6 is $ipv6" | |
# if here, the dns record needs updating | |
# get the zone id for the requested zone | |
zoneid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone&status=active" -H "X-Auth-Email: $cloudflare_auth_email" -H "X-Auth-Key: $cloudflare_auth_key" -H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') | |
echo "Zoneid for $zone is $zoneid" | |
# get the dns record id v4 | |
dnsrecordidv4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" -H "X-Auth-Email: $cloudflare_auth_email" -H "X-Auth-Key: $cloudflare_auth_key" -H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') | |
echo "DNSrecordid IPv4 for $dnsrecord is $dnsrecordidv4" | |
# update the record v4 | |
responsev4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordidv4" -H "X-Auth-Email: $cloudflare_auth_email" -H "X-Auth-Key: $cloudflare_auth_key" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"$dnsrecord\",\"content\":\"$ipv4\",\"ttl\":1,\"proxied\":true}") | |
# get the dns record id v6 | |
dnsrecordidv6=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=AAAA&name=$dnsrecord" -H "X-Auth-Email: $cloudflare_auth_email" -H "X-Auth-Key: $cloudflare_auth_key" -H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') | |
echo "DNSrecordid IPv6 for $dnsrecord is $dnsrecordidv6" | |
# update the record v6 | |
responsev6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordidv6" -H "X-Auth-Email: $cloudflare_auth_email" -H "X-Auth-Key: $cloudflare_auth_key" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"$dnsrecord\",\"content\":\"$ipv6\",\"ttl\":1,\"proxied\":true}") | |
if [ "$responsev4" != "${responsev4%success*}" ] && [ "$(echo $responsev4 | grep "\"success\":true")" != "" ]; then | |
echo "Updated to $ipv4 succesfuly!" | |
else | |
echo 'Something went wrong :(' | |
echo "Response: $responsev4" | |
fi | |
if [ "$responsev6" != "${responsev6%success*}" ] && [ "$(echo $responsev6 | grep "\"success\":true")" != "" ]; then | |
echo "Updated to $ipv6 succesfuly!" | |
else | |
echo 'Something went wrong :(' | |
echo "Response: $responsev6" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment