Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Cron it for every 5 minutes.
- */5 * * * * /opt/scripts/ddns_updater.sh
- #!/bin/bash
- # Set these variables
- ZONE_ID="XXXXXXX" #Find this under the API section of your root DNS
- DNS_ID="XXXXXX" #This one is tricky to find. Make an edit to the DNS you want to be dynamic > From Homepage > Manage Account > Audit Logs > Find the edit you made < Click the log > Grab the resource ID
- API_TOKEN="XXXXXXX"
- #To get your API token > Sign in to CF >Pick your Domain you want DDNS on > To your bottom right find API and click "Get API TOKEN" >Click Create API Token
- #Permission 1 needed ZONE > DNS Settings > Edit
- #Permission 2 needed ZONE > DNS > Edit
- #Include your zone (Root URL) > Save it and your API Key
- RECORD_NAME="external-link-url" #The domain or subdomain you want to update. I Cname everything to a primary point and then have that primary point be DDNS
- TIMESTAMP=$(date -R)
- # File to store last known IP
- IP_FILE="/var/tmp/current_ip.txt"
- # Get current public IP
- CURRENT_IP=$(curl -s https://api.ipify.org)
- # Check if IP file exists and read last IP
- if [[ -f "$IP_FILE" ]]; then
- LAST_IP=$(cat "$IP_FILE")
- else
- LAST_IP=""
- fi
- # Compare and update if changed
- if [[ "$CURRENT_IP" != "$LAST_IP" ]]; then
- echo "IP changed: $LAST_IP -> $CURRENT_IP"
- echo "$CURRENT_IP" > "$IP_FILE"
- # Update Cloudflare DNS record
- curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_ID" \
- -H "Content-Type: application/json" \
- -H "Authorization: Bearer $API_TOKEN" \
- -d "{
- \"comment\": \"Updated on $TIMESTAMP\",
- \"content\": \"$CURRENT_IP\",
- \"name\": \"$RECORD_NAME\",
- \"proxied\": true,
- \"ttl\": 3600,
- \"type\": \"A\"
- }"
- fi
Advertisement
Add Comment
Please, Sign In to add comment