Skip to content

Instantly share code, notes, and snippets.

@martin2844
Created October 6, 2025 17:56
Show Gist options
  • Select an option

  • Save martin2844/3b369cb59d3cfb33b133db84991bdb1e to your computer and use it in GitHub Desktop.

Select an option

Save martin2844/3b369cb59d3cfb33b133db84991bdb1e to your computer and use it in GitHub Desktop.
Update cloudflare IPs from remote server
#!/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