Skip to content

Instantly share code, notes, and snippets.

@4ft35t
Forked from benkulbertis/cloudflare-update-record.sh
Last active December 4, 2022 15:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.
Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.
Cloudflare API v4 Dynamic DNS Update in Bash
#!/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}'
@yanwen
Copy link

yanwen commented Jul 14, 2019

When I run it in busybox under router. Some errors happen below:

API UPDATE FAILED. DUMPING RESULTS:

@4ft35t
Copy link
Author

4ft35t commented Jul 16, 2019

When I run it in busybox under router. Some errors happen below:

API UPDATE FAILED. DUMPING RESULTS:

paste cloudflare.log content here

@yanwen
Copy link

yanwen commented Jul 16, 2019

[Mon Jul 15 02:04:25 CST 2019] - Check Initiated
[Mon Jul 15 02:04:26 CST 2019] - API UPDATE FAILED. DUMPING RESULTS:

[Mon Jul 15 02:11:10 CST 2019] - Check Initiated
[Mon Jul 15 02:11:11 CST 2019] - API UPDATE FAILED. DUMPING RESULTS:

[Mon Jul 15 02:22:20 CST 2019] - Check Initiated
[Mon Jul 15 02:22:22 CST 2019] - API UPDATE FAILED. DUMPING RESULTS:

@4ft35t
Copy link
Author

4ft35t commented Jul 18, 2019

rm cloudflare.ids, and replace 4-7 line to your own info.

@deeco
Copy link

deeco commented Jul 26, 2019

How to run this correctly ? Im getting API UPDATE FAILED. DUMPING RESULTS:
{"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}

command trying is ./cloudflare-update-record.sh and ./cloudflare-update-record.sh -ip=192.168.1.1

@4ft35t
Copy link
Author

4ft35t commented Jul 29, 2019

How to run this correctly ? Im getting API UPDATE FAILED. DUMPING RESULTS:
{"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}

command trying is ./cloudflare-update-record.sh and ./cloudflare-update-record.sh -ip=192.168.1.1

./cloudflare-update-record.sh works for me. And this scripts run without any args.
To debug it, run with
bash -x cloudflare-update-record.sh

@chantroiviet
Copy link

Please update. I update for https://ctviet.com but error alert: API UPDATE FAILED. DUMPING RESULTS

@4ft35t
Copy link
Author

4ft35t commented Apr 14, 2020

Please update. I update for https://ctviet.com but error alert: API UPDATE FAILED. DUMPING RESULTS

Works for me. Please paste the next line after API UPDATE FAILED. DUMPING RESULTS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment