Skip to content

Instantly share code, notes, and snippets.

@BrianMitchL
Last active March 1, 2023 06:51
Show Gist options
  • Save BrianMitchL/987ec33cabb81f176644b53810dd0bdc to your computer and use it in GitHub Desktop.
Save BrianMitchL/987ec33cabb81f176644b53810dd0bdc to your computer and use it in GitHub Desktop.
A bash script to update an A record for a domain in Cloudflare to be the value of the current IP
#!/bin/bash
zone_id=
# needs DNS read and write access
api_key=
# must also be in the given zone
record_name=example.com
ip=$(curl -s -X GET https://checkip.amazonaws.com)
record_id=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?type=A&name=$record_name" \
-H "Authorization: Bearer $api_key" \
-H "Content-Type:application/json" | jq -r '{"result"}[] | .[0] | .id')
curl -s -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" \
--data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment