Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bestrocker221/5fa7b252e5d2fa75fc46e8eb67f85cac to your computer and use it in GitHub Desktop.
Save bestrocker221/5fa7b252e5d2fa75fc46e8eb67f85cac to your computer and use it in GitHub Desktop.
Dynamic DNS record update through Cloudflare APIs
#!/bin/bash
ZONE_ID=<YOUR_ZONE_ID>
A_record=<domain/subdomain>
A_record_id=<RECORD ID>
CF_BEARER=<YOUR_CF_DNS_TOKEN>
LOG_FILE="./ip_log.txt"
DNS_UPDATE_LOG_FILE="./update_dns_history.log"
IP=$(curl -s https://ipinfo.io/ip)
if [[ -n "$PUBLIC_IP" ]]; then
DATE_TIME=$(date +"%Y-%m-%d %H:%M:%S")
LAST_IP=$(tail -n 1 "$LOG_FILE" | awk '{print $4}')
if [ "$LAST_IP" != "$IP" ]; then
DATA_TO_UPDATE="{\"type\":\"A\",\"name\":\"$A_record\",\"content\":\"$IP\",\"ttl\":1,\"proxied\":false}"
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/"$ZONE_ID"/dns_records/"$A_record_id -H "Authorization: Bearer ${CF_BEARER}" -H "Content-Type:application/json" --data $DATA_TO_UPDATE >> ${DNS_UPDATE_LOG_FILE}
echo "${DATE_TIME} - ${PUBLIC_IP}" >> "$LOG_FILE"
fi
fi
# get records
#curl https://api.cloudflare.com/client/v4/zones/"$ZONE_ID"/dns_records -H "Authorization: Bearer ${CF_BEARER}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment