Skip to content

Instantly share code, notes, and snippets.

@AkashRajpurohit
Created March 5, 2023 05:57
Show Gist options
  • Save AkashRajpurohit/dcadf2b1a180fac4227b5b4855773917 to your computer and use it in GitHub Desktop.
Save AkashRajpurohit/dcadf2b1a180fac4227b5b4855773917 to your computer and use it in GitHub Desktop.
Cloudflare DDNS
#!/bin/bash
# More details here: https://akashrajpurohit.com/blog/dynamic-dns-made-easy-with-cloudflare-api/
# Define your Cloudflare API key and email
CLOUDFLARE_API_KEY=your_api_key
CLOUDFLARE_EMAIL=your_email
# Define the domain and record you want to update
DOMAIN=your_domain.com
RECORD=your_record
# Get the current public IP address
IP=$(curl -s https://cloudflare.com/cdn-cgi/trace | grep -E '^ip' | cut -d = -f 2)
# Get the current IP address on Cloudflare
CF_IP=$(curl -s https://api.cloudflare.com/client/v4/zones/$DOMAIN/dns_records/$RECORD \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
| jq '.result.content' \
| tr -d \")
# Update the IP address on Cloudflare if it has changed
if [ "$IP" != "$CF_IP" ]; then
curl -s https://api.cloudflare.com/client/v4/zones/$DOMAIN/dns_records/$RECORD \
-X PUT \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$RECORD\",\"content\":\"$IP\"}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment