If you are looking for a simple script that would update a cloudflare dns entry for you like a DDNS service provider, search no longer.
#!/bin/bash
token="<your cloudflare api token>"
zone_id="<the zone id>"
dns_record_id4="<dns record id for ipv4>"
dns_record_id6="<dns record id for ipv6>"
ipv4=$(curl -s https://api.ipify.org)
ipv6=$(curl -s https://api6.ipify.org)
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$dns_record_id4 \
-X PUT \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $token" \
-d '{
"name": "homesweethome.mydomain.com",
"ttl": 3600,
"type": "A",
"content": "'"$ipv4"'"
}'
curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$dns_record_id6 \
-X PUT \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $token" \
-d '{
"name": "homesweethome.mydomain.com",
"ttl": 3600,
"type": "AAAA",
"content": "'"$ipv6"'"
}'
To acquire the zone and dns record ids, you can run the following commands:
curl https://api.cloudflare.com/client/v4/zones -H "Authorization: Bearer <your cloudflare api token> | jq"
This will give you a nicely formatted list of all the DNS zones the api key has access to. Once you figured out which ID belongs to the domain you need to update, you can run the following command to obtain the record ids:
curl https://api.cloudflare.com/client/v4/zones/<zone_id>/dns_records -H "Authorization: Bearer <your cloudflare api token>" | jq
If your isp does not provide you with an ipv4 or ipv6 address, or if you don't want to use either of them, you can remove all the version references.
Put this in your crontab, system boot scripts or NetworkManager's dispatch directory to automate updating your records.
Add comment »