A Python script to automatically update Cloudflare DNS records with your dynamic IP, supporting proxy toggling and multiple records.
- Automatically fetches the current public IP using the
ipify
service. - Dynamically retrieves DNS record IDs from Cloudflare, removing manual setup requirements.
- Updates multiple DNS records based on configuration.
- Supports enabling or disabling Cloudflare Proxy (
proxied
option) for each record. - Runs continuously, checking for IP changes at regular intervals.
- Python 3.7 or higher
requests
library
git clone https://github.com/yourusername/cloudflare-dynamic-ip-updater.git
cd cloudflare-dynamic-ip-updater
Install the required Python library:
pip install reqirements.txt
Use the provided config.json.example
as a template:
cp config.json.example config.json
Edit config.json with your details:
{
"records": [
{
"api_token": "your_api_token",
"zone_id": "your_zone_id",
"record_name": "example.com",
"proxied": true
}
]
}
api_token: Your Cloudflare API Token with permissions for DNS Zone and DNS Records.
zone_id: The Zone ID of your domain in Cloudflare.
record_name: The DNS record (e.g., example.com or sub.example.com).
proxied: true to enable Cloudflare Proxy, false to disable it.
Run the script to start updating your DNS records:
python main.py
The script will fetch your current public IP and update the specified DNS records if the IP changes.
To keep the script running 24/7, use a process manager like screen, tmux, or a system service.
screen -S ip-updater python cloudflare_ip_updater.py
Create a service file (e.g., /etc/systemd/system/cloudflare-ip-updater.service
):
[Unit]
Description=Cloudflare Dynamic IP Updater
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/cloudflare_ip_updater.py
Restart=always
User=your_username
WorkingDirectory=/path/to/repo
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl enable cloudflare-ip-updater
sudo systemctl start cloudflare-ip-updater
config.json
:
{
"records": [
{
"api_token": "your_api_token",
"zone_id": "your_zone_id",
"record_name": "example.com",
"proxied": true
},
{
"api_token": "another_api_token",
"zone_id": "another_zone_id",
"record_name": "sub.example.com",
"proxied": false
}
]
}