Skip to content

Instantly share code, notes, and snippets.

@cysk003
Forked from phlinhng/cloudflare-ddns.sh
Created May 14, 2023 23:50
Show Gist options
  • Select an option

  • Save cysk003/57fc1ecc0cdd3bb1b6111850cc2bb209 to your computer and use it in GitHub Desktop.

Select an option

Save cysk003/57fc1ecc0cdd3bb1b6111850cc2bb209 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