Last active
October 13, 2022 19:31
-
-
Save ssgtcookie/5e6c0039308b386f89e1d73f70761235 to your computer and use it in GitHub Desktop.
Batch: Cloudflare APIv4 dynamic update A-record script. Created for a Synology NAS hosted Wordpress website.
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
# set shell variables with your contents | |
email="[email protected]" | |
authKey="" | |
zoneid="" | |
dnsrecord="" | |
domain="example.com" | |
# Collect current IP | |
current_ip=$(curl --silent --show-error --fail ipecho.net/plain) || exit | |
# ...and submit | |
curl --silent --show-error --fail -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecord" \ | |
-H "X-Auth-Email: $email" \ | |
-H "X-Auth-Key: $authKey" \ | |
-H "Content-Type: application/json" \ | |
--data @- <<END; | |
{ | |
"id": "$zoneid", | |
"type": "A", | |
"name": "$domain", | |
"content": "$current_ip", | |
"zone_name": "$domain" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What this script does
This script will update your A-record on Cloudflare with your current public IP-address every time it runs. The script does not monitor for IP-address changes so you might want to schedule this script to run every minute/hour/day to prevent downtime when your IP-address changes. The CloudFlare API sets a maximum of 1,200 requests in a five minute period. You might want to use this script if your ISP is giving you a dynamic IP-address. If you're using a Synology NAS, you can use the build-in scheduler in DSM (6.0) to schedule this script.
The script is written in pure bash (.sh script) so no need for python/perl/bash 3rd party extensions or sudo rights.
Instructions
curl -X GET https://api.cloudflare.com/client/v4/zones -H "X-Auth-Email:" -H "X-Auth-Key: xxxxxx" -H "Content-Type: application/json"