Created
March 5, 2023 05:57
-
-
Save AkashRajpurohit/dcadf2b1a180fac4227b5b4855773917 to your computer and use it in GitHub Desktop.
Cloudflare DDNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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