Use this script as a template for managing dynamic IP addresses in Cloudflare DNS using API v4.
Documentation about updating a DNS record through Cloudflare API v4 is available here: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record.
-
Read the documentation of the API and generate your authentication token.
-
Get your DNS zone's ID from your dashboard.
-
Get your DNS record's ID using the API:
$ curl -X GET "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer TOKEN"
- Install
jq
JSON parser:
# apt install jq
- Download the script file
cloudflare-dynamic-ip.sh
and fill in all the settings:
- Authentication
TOKEN
ZONE_ID
- Common record settings
ZONE_NAME
RECORD_TYPE
RULE_PROXIABLE
(defaulttrue
)RULE_PROXIED
(defaulttrue
)RULE_LOCKED
(defaultfalse
)STORED_IP
- Specific record settings
RECORD_ID
RECORD_NAME
- Create a
cloudflare-dynamic-ip-last.txt
for storing the last IP and preventing from calling the API if there is no change. Ensure that the user running the script has permission to read and write this file.
Run the script:
$ bash cloudflare-dynamic-ip.sh
If you want to run the script programatically, you can use Cron jobs:
# crontab -e
And add, for example:
0,14,29,44 * * * * /bin/bash /home/user/cloudflare-dynamic-ip.sh