Created
October 6, 2025 17:56
-
-
Save martin2844/3b369cb59d3cfb33b133db84991bdb1e to your computer and use it in GitHub Desktop.
Update cloudflare IPs from remote server
This file contains hidden or 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 | |
| # === CONFIG === | |
| CF_API_TOKEN="token" | |
| CF_ZONE_ID="id" | |
| CF_RECORD_ID="record_id" | |
| CF_RECORD_NAME="el_name" | |
| # === GET IPs === | |
| CURRENT_IP=$(curl -s https://ipv4.icanhazip.com | tr -d '\n') | |
| DNS_IP=$(dig +short "$CF_RECORD_NAME" @1.1.1.1) | |
| # === COMPARE === | |
| if [ "$CURRENT_IP" = "$DNS_IP" ]; then | |
| echo "[✓] IP unchanged: $CURRENT_IP" | |
| exit 0 | |
| fi | |
| # === UPDATE === | |
| echo "[!] IP changed: $DNS_IP → $CURRENT_IP" | |
| RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| --data "{\"type\":\"A\",\"name\":\"$CF_RECORD_NAME\",\"content\":\"$CURRENT_IP\",\"ttl\":300,\"proxied\":false}") | |
| echo "$RESPONSE" | |
| # Hacerlo ejecutable -> chmod +x ~/update-cloudflare-ip.sh | |
| # agregarlo a crontab -> crontab -e | |
| # corriendo cada 5 min -> */5 * * * * /home/martin/update-cloudflare-ip.sh >> /var/log/cloudflare-ddns.log 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment