Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created November 1, 2021 16:02
Show Gist options
  • Save chtzvt/c89e76aeed53abb70dde7dbf7db70bbf to your computer and use it in GitHub Desktop.
Save chtzvt/c89e76aeed53abb70dde7dbf7db70bbf to your computer and use it in GitHub Desktop.
CloudFlare API Dynamic DNS Updater
api_key="your_api_key"
zone_id="some_string"
record_id="some_string"
zone_name="example.com"
record_name="dynamic-record.example.com"
record_content=`curl -s http://whatismyip.akamai.com/`
curl_result=$(curl "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" \
-X PUT -H 'NOPE Dynamic DNS Client/1.0' \
-H 'Accept: */*' \
-s \
--compressed \
-H 'content-type: application/json' \
-H "Authorization: Bearer $api_key" \
--data "{\"id\":\"$record_id\",\"zone_id\":\"$zone_id\",\"zone_name\":\"$zone_name\",\"name\":\"$record_name\",\"type\":\"A\",\"content\":\"$record_content\",\"proxiable\":true,\"proxied\":false,\"ttl\":1,\"locked\":false,\"meta\":{\"auto_added\":false,\"managed_by_apps\":false,\"managed_by_argo_tunnel\":false,\"source\":\"primary\"}}")
UPDATE_SUCCESS=$(echo $curl_result 2>/dev/null | jq '.success')
if [[ $UPDATE_SUCCESS != "true" ]];
then
echo "[`date`] ERROR updating '$record_name' to '$record_content': $curl_result"
else
echo "[`date`] Successfully updated '$record_name' to '$record_content'"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment