Skip to content

Instantly share code, notes, and snippets.

@palemoky
Last active July 8, 2020 05:08
Show Gist options
  • Save palemoky/6b0a3a44efc247c5d713205594eae392 to your computer and use it in GitHub Desktop.
Save palemoky/6b0a3a44efc247c5d713205594eae392 to your computer and use it in GitHub Desktop.
Cloudflare DDNS
#!/bin/bash
# Author: Palemoky
email=
domain=
api_token=
dns_identifier=
zone_identifier=
# Get dns record of cloudflare
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns_identifier" -H "X-Auth-Email: $email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" | jq .result.content)
# Get current ip
ip=$(curl -s whatismyip.akamai.com)
# Edit records only when IP changes
if [ ${record//\"/} != $ip ]; then
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns_identifier" -H "X-Auth-Email: $email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" --data '{"type":"A","name":"'$domain'","content":"'$ip'","ttl":1,"proxied":false}'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment