Skip to content

Instantly share code, notes, and snippets.

@phlinhng
Last active September 5, 2023 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save phlinhng/4f043630d2e83cdd3948078079b0d0de to your computer and use it in GitHub Desktop.
Save phlinhng/4f043630d2e83cdd3948078079b0d0de to your computer and use it in GitHub Desktop.
Cloudflare DDNS Script
#!/bin/bash
zone_name=$1
record_name=$2
api_key=$3
current_ip=`curl -s https://api.ipify.org`
zone_id=`curl -s -X GET "https://api.cloudflare.com/client/v4/zones" \
-H "Authorization: Bearer ${api_key}" -H "Content-Type: application/json" \
| jq -r ".result | .[] | select(.name == \"${zone_name}\") | .id"`
record_id=`curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records" \
-H "Authorization: Bearer ${api_key}" -H "Content-Type: application/json" \
| jq -r ".result | .[] | select(.name == \"${record_name}.${zone_name}\") | .id"`
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}" \
-H "Authorization: Bearer ${api_key}" -H "Content-Type: application/json" \
-d "{\"type\": \"A\", \"name\": \"${record_name}\", \"content\": \"${current_ip}\"}"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment