Skip to content

Instantly share code, notes, and snippets.

@ssgtcookie
Last active October 13, 2022 19:31
Show Gist options
  • Save ssgtcookie/5e6c0039308b386f89e1d73f70761235 to your computer and use it in GitHub Desktop.
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.
# 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"
}
@ssgtcookie
Copy link
Author

ssgtcookie commented May 28, 2016

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

  1. Insert your registered Cloudflare e-mail.
  2. Insert your authkey. You can find your authkey by logging in to your personal Cloudflare account. The authkey is listed in your Cloudflare account settings.
  3. Insert your Zone-ID. The Zone-ID is unique for every domain that you're using with Cloudflare. Use the following command on the terminal to retreive your zone-IDs.

curl -X GET https://api.cloudflare.com/client/v4/zones -H "X-Auth-Email:" -H "X-Auth-Key: xxxxxx" -H "Content-Type: application/json"

  1. Insert your DNS record. Te DNS record can also be found when executing the above command.
  2. Insert the domain you want to target with this script like example.com.

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