Skip to content

Instantly share code, notes, and snippets.

@mjhirst
Last active June 8, 2023 21:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mjhirst/44796dc4558f5f7e053e0e2d838c621b to your computer and use it in GitHub Desktop.
Save mjhirst/44796dc4558f5f7e053e0e2d838c621b to your computer and use it in GitHub Desktop.
Use Cloudflare's API to set Dynamic DNS records with Crontab
#!/bin/sh
# Cloudflare API v.4 Variables
CF_APIKEY='Your API Key Here'
CF_ZONEID='The Zone ID here' # Found on your Cloudflare Dashboard
CF_DNSID='The DNS ID here' # Found by listing DNS with Cloudflare API, see below for command
CF_EMAIL='your@email.address'
CF_DNS='address.domain.com'
GET_IP=$(dig +short txt ch whoami.cloudflare @1.0.0.1)
# Make the request
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONEID}/dns_records/${CF_DNSID}" \
-H "X-Auth-Email: ${CF_EMAIL}" \
-H "X-Auth-Key: ${CF_APIKEY}" \
-H "Content-Type: application/json" \
-d $'{"type":"A",
"name":"'${CF_DNS}'",
"content":'$GET_IP',
"ttl":1,
"proxied":false }'
# Optional, add to Crontab
# This runs every 5 minutes
# Set SCRIPT_LOCATION and LOGS_LOCATION to an absolute path like /home/username
#
# 0/5 * * * * /bin/sh SCRIPT_LOCATION/cloudflare_ddns.sh > LOGS_LOCATION/ddns.log 2>&1
# Optional, find Zone ID
#
# curl -X GET "https://api.cloudflare.com/client/v4/zones/{CF_ZONEID}/dns_records" \
# -H "Content-Type:application/json" \
# -H "X-Auth-Key:${CF_APIKEY}" \
# -H "X-Auth-Email:${CF_EMAIL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment