Forked from Ciantic/cloudflare-dyndns-a-aaaa-record-update.sh
Last active
February 3, 2023 11:28
-
-
Save apparition47/2a09162ebfed455b06119535feafa3cb to your computer and use it in GitHub Desktop.
cloudflare dyndns script / update a A and AAAA record using bash script
This file contains hidden or 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 | |
# Author: Jari Pennanen | |
# Url: https://gist.github.com/Ciantic/4e543f2d878a87a38c25032d5c727bf2 | |
# create your API Key at https://dash.cloudflare.com/profile/api-tokens | |
# Required permissions: Zone Settings:Read, Zone:Read, DNS:Edit (all under zone) | |
AUTH_KEY="" # Get this from My profile -> API Keys -> View | |
DOMAIN="example.com" # main domain | |
SUBDOMAIN="home.example.com" # set A and AAAA-record of this subdomain | |
ZONE_ID=$(\ | |
curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN&status=active" \ | |
-H "Authorization: Bearer $AUTH_KEY" \ | |
-H "Content-Type: application/json" \ | |
| \ | |
python3 -c "import sys, json; print(json.load(sys.stdin)['result'][0]['id'])" \ | |
) | |
if [[ -z "$ZONE_ID" ]]; then | |
echo "Zone ID can't be determined" 1>&2 | |
exit 1 | |
fi | |
echo "Zone ID:" $ZONE_ID | |
RECORD_IPV4_ID=$(\ | |
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$SUBDOMAIN" \ | |
-H "Authorization: Bearer $AUTH_KEY" \ | |
-H "Content-Type: application/json" \ | |
| \ | |
python3 -c "import sys, json; print(json.load(sys.stdin)['result'][0]['id'])" \ | |
) | |
if [[ -z "$RECORD_IPV4_ID" ]]; then | |
echo "Record ID can't be determined" 1>&2 | |
exit 1 | |
fi | |
echo "A-Record ID:" $RECORD_IPV4_ID | |
RECORD_IPV6_ID=$(\ | |
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=AAAA&name=$SUBDOMAIN" \ | |
-H "Authorization: Bearer $AUTH_KEY" \ | |
-H "Content-Type: application/json" \ | |
| \ | |
python3 -c "import sys, json; print(json.load(sys.stdin)['result'][0]['id'])" \ | |
) | |
if [[ -z "$RECORD_IPV6_ID" ]]; then | |
echo "Record ID can't be determined" 1>&2 | |
exit 1 | |
fi | |
echo "AAAA-Record ID:" $RECORD_IPV6_ID | |
# curl -s -4 https://ifconfig.co/ip \ | |
IPV4_ADDRESS=$(\ | |
curl -s https://ipv4.icanhazip.com \ | |
) | |
if [[ -z "$IPV4_ADDRESS" ]]; then | |
echo "IPV4 Address can't be determined" 1>&2 | |
exit 1 | |
fi | |
# curl -s -6 https://ifconfig.co/ip \ | |
IPV6_ADDRESS=$(\ | |
curl -s https://ipv6.icanhazip.com \ | |
) | |
if [[ -z "$IPV6_ADDRESS" ]]; then | |
echo "IPV6 Address can't be determined" 1>&2 | |
exit 1 | |
fi | |
echo "IP Address: " $IPV4_ADDRESS " / " $IPV6_ADDRESS | |
# Note: TTL=1 automatic | |
IPV4_UPDATE_RESULT=$(\ | |
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_IPV4_ID" \ | |
-H "Authorization: Bearer $AUTH_KEY" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"A","name":"'"$SUBDOMAIN"'","content":"'"$IPV4_ADDRESS"'","ttl":1,"proxied":false}' \ | |
| \ | |
python3 -c "import sys, json; print(json.load(sys.stdin)['success'])" \ | |
) | |
if [[ "$IPV4_UPDATE_RESULT" -ne "True" ]]; then | |
echo "IPV4 Address can't be set" 1>&2 | |
exit 1 | |
fi | |
IPV6_UPDATE_RESULT=$(\ | |
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_IPV6_ID" \ | |
-H "Authorization: Bearer $AUTH_KEY" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"AAAA","name":"'"$SUBDOMAIN"'","content":"'"$IPV6_ADDRESS"'","ttl":1,"proxied":false}' \ | |
| \ | |
python3 -c "import sys, json; print(json.load(sys.stdin)['success'])" \ | |
) | |
if [[ "$IPV6_UPDATE_RESULT" -ne "True" ]]; then | |
echo "IPV6 Address can't be set" 1>&2 | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment