Can be scheduled to run periodically using Cron jobs.
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://developers.cloudflare.com/api/operations/dns-records-for-a-zone-update-dns-record.
-
Generate a token for the Cloudflare API. For mor details, see https://developers.cloudflare.com/fundamentals/api/get-started/create-token/.
-
Verify the token:
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer {token}" \
-H "Content-Type:application/json"
-
Get your DNS zone's ID from your dashboard. For more details, see https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/.
-
Get your DNS record's details using the API:
- Only records of type
A
(IPv4) are supported by this script.
- Only records of type
curl -X GET "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" \
-H "Authorization: Bearer {token}"
-
Copy the
config/config.sample.py
file toconfig/config.py
and fill in the required information. -
Install the required Python packages:
pip install -r requirements.txt
- Test the script:
python cloudflare-dynamic-ip.py
It should generate a log file called cloudflare-dynamic-ip.log
in the same directory.
Run the script:
python cloudflare-dynamic-ip.py
If you want to run the script programatically, you can use Cron jobs:
crontab -e
And add, for example:
@reboot /path/to/.venv/bin/python /path/to/cloudflare-dynamic-ip.py
0 0,12 * * * /path/to/.venv/bin/python /path/to/cloudflare-dynamic-ip.py
This Cron configuration will run the script at reboot and every day at 00:00 and 12:00.