Skip to content

Instantly share code, notes, and snippets.

@codeThatThinks
Created November 28, 2020 04:52
Show Gist options
  • Save codeThatThinks/b4ddc48a97c47a0edd24fb32dcbd2a91 to your computer and use it in GitHub Desktop.
Save codeThatThinks/b4ddc48a97c47a0edd24fb32dcbd2a91 to your computer and use it in GitHub Desktop.
Cloudflare Dynamic DNS Script

Cloudflare Dynamic DNS

  1. Create an API token. Go to Profile > API Tokens and create a token.

  2. Use the API to list your zones to deterimine ID of the zone you want to update.

  3. Use the API to list your zone records to determine the ID of the record(s) you want to update.

  4. Use the following bash script to update these records, filling in your API token, zone id, and record ids:

#!/bin/bash

api_token="YOUR API TOKEN"
zone="YOUR ZONE ID"
records=("YOUR 1st RECORD ID" "YOUR 2nd RECORD ID" "YOUR 3rd RECORD ID")
public_ip=$(curl -s "http://ipecho.net/plain")

echo "Public IP is ${public_ip}"

for record in "${records}"; do
	echo "Updating ${zone} -> ${record}"
	curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records/${record}" \
	-H "Authorization: Bearer ${api_token}" \
	-H "Content-Type:application/json" \
	--data "{\"content\":\"${public_ip}\"}"
	echo ""
done
  1. Create a systemd timer or cron job to run this script periodically, e.g. every hour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment