Skip to content

Instantly share code, notes, and snippets.

@15cm
Forked from benkulbertis/cloudflare-update-record.sh
Last active February 18, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 15cm/af7fc9d919917a30d4afaef911f49321 to your computer and use it in GitHub Desktop.
Save 15cm/af7fc9d919917a30d4afaef911f49321 to your computer and use it in GitHub Desktop.
Cloudflare API v4 Dynamic DNS Update in sh(For ASUS Merlin)
#!/bin/bash
# https://github.com/RMerl/asuswrt-merlin/wiki/Custom-DDNS
# CHANGE THESE
auth_email="user@example.com"
auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare account settings
zone_name="example.com"
record_names="*.a.example.com"
record_names="$record_names a.example.com"
ip=$(curl -s https://ipv4.icanhazip.com)
for record_name in $record_names; do
# compatible with grep without perl support
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -o '"id":"[0-9a-z]*"' | grep -o '"[0-9a-f]*"' | grep -o '[0-9a-f]*' | head -1 )
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -o '"id":"[0-9a-z]*"' | grep -o '"[0-9a-f]*"' | grep -o '[0-9a-f]*')
curl -fs -o /dev/null -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"proxied\":false}"
done
if [ $? -eq 0 ]; then
/sbin/ddns_custom_updated 1
else
/sbin/ddns_custom_updated 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment