Skip to content

Python script to manage dynamic IP addresses in Cloudflare DNS

License

Notifications You must be signed in to change notification settings

juancrrn/python-cloudflare-dynamic-ip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python script to manage dynamic IP addresses in Cloudflare DNS

Can be scheduled to run periodically using Cron jobs.

Introduction

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.

Installation

  1. Generate a token for the Cloudflare API. For mor details, see https://developers.cloudflare.com/fundamentals/api/get-started/create-token/.

  2. Verify the token:

curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
     -H "Authorization: Bearer {token}" \
     -H "Content-Type:application/json"
  1. Get your DNS zone's ID from your dashboard. For more details, see https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/.

  2. Get your DNS record's details using the API:

    • Only records of type A (IPv4) are supported by this script.
curl -X GET "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" \
     -H "Authorization: Bearer {token}"
  1. Copy the config/config.sample.py file to config/config.py and fill in the required information.

  2. Install the required Python packages:

pip install -r requirements.txt
  1. Test the script:
python cloudflare-dynamic-ip.py

It should generate a log file called cloudflare-dynamic-ip.log in the same directory.

Usage

Run the script:

python cloudflare-dynamic-ip.py

Cron

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.

About

Python script to manage dynamic IP addresses in Cloudflare DNS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages