Forked from benkulbertis/cloudflare-update-record.sh
Last active
December 4, 2022 15:55
-
-
Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.
Cloudflare API v4 Dynamic DNS Update in Bash
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/sh | |
## edit this | |
cf_ddns_host=myddns.mydomain.com | |
# My Profile - API Tokens - Create Token - Use template of Edit zone DNS - Zone Resources - Include - All zones - Continue to summary - Create Token | |
cf_token= | |
## edit end | |
# auto fetch | |
cf_zoneid= | |
cf_recordid= | |
servers=' | |
http://myip.ipip.net/s | |
http://ipinfo.io/ip | |
http://ipecho.net/plain | |
http://members.3322.org/dyndns/getip | |
http://ip.sb | |
http://ifconfig.me | |
' | |
cfcurl(){ | |
curl -H "Authorization: Bearer $cf_token" -H "Content-Type: application/json" $@ | |
} | |
# get root domain | |
get_cf_ids(){ | |
domain=$(curl "http://223.5.5.5/resolve?name=$cf_ddns_host&type=ns" | tr , '\n' | awk -F'"' '/Authority/{print $(NF-1)}') | |
cf_zoneid=$(cfcurl "https://api.cloudflare.com/client/v4/zones?name=$domain" \ | |
| tr , '\n' \ | |
| awk -F'"' '/"id"/{print $(NF-1)}' \ | |
| head -1 | |
) | |
cf_recordid=$(cfcurl -X GET "https://api.cloudflare.com/client/v4/zones/$cf_zoneid/dns_records?type=A&name=$cf_ddns_host" \ | |
| tr , '\n' \ | |
| awk -F'"' '/"id"/{print $(NF-1)}' \ | |
| head -1 | |
) | |
sed -i "s/^cf_zoneid=.*$/cf_zoneid=$cf_zoneid/" $0 | |
sed -i "s/^cf_recordid=.*$/cf_recordid=$cf_recordid/" $0 | |
} | |
for s in $servers | |
do | |
ip=$(curl -s -m3 $s) | |
[ "$ip" ] && break | |
done | |
[ "$cf_recordid" ] || get_cf_ids | |
cfcurl -X PUT "https://api.cloudflare.com/client/v4/zones/$cf_zoneid/dns_records/$cf_recordid" \ | |
--data '{"type":"A","name":"'$cf_ddns_host'","content":"'$ip'","ttl":3600,"proxied":false}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for me. Please paste the next line after
API UPDATE FAILED. DUMPING RESULTS