Skip to content

Instantly share code, notes, and snippets.

@hugo4715
Forked from mjhirst/cloudflare_ddns.sh
Created February 25, 2023 09:30
Show Gist options
  • Save hugo4715/71d9955bbf63a8302ca72cdf2a73cbd2 to your computer and use it in GitHub Desktop.
Save hugo4715/71d9955bbf63a8302ca72cdf2a73cbd2 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='[email protected]'
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