This Python script automates the process of updating DNS records for specific subdomains under Cloudflare. It fetches the current public IP address (both IPv4 and IPv6) and compares it with the existing IP recorded in Cloudflare. If a change is detected, it updates the DNS record accordingly.
- Python 3.x installed
- Cloudflare API key
- Configured
config.json
file containing necessary configurations
- Clone or download this repository.
- Ensure Python 3.x is installed.
- Install the required dependencies using
pip install -r requirements.txt
.
To use this script, you'll need a Cloudflare API key. Here are the steps to obtain it:
-
Login to Cloudflare: Visit Cloudflare and log in to your account.
-
Access API Tokens: Go to the "My Profile" section or navigate to "Account" > "API Tokens" in the Cloudflare dashboard.
-
Create API Token: Click on "Create Token" or "Create Token" button to generate a new API token.
-
Select Permissions: Choose the appropriate permissions for your token. At a minimum, it requires permissions to edit DNS records.
-
Generate API Key: Once permissions are set, generate the API key. It will be displayed once and should be copied and securely stored.
-
Add API Key to Configuration: Paste the generated API key into the
api_key
field within theconfig.json
file for this script. -
Save and Secure: Ensure the API key is saved securely. Do not expose or share it publicly.
Refer to Cloudflare's official documentation for more detailed instructions on generating and managing API tokens.
- Create a
config.json
file based on the providedconfig.json.example
with your Cloudflare API key and domain details. - Populate the
config.json
file with your specific domain, subdomain, record type, and proxied status.
- Ensure
config.json
is properly configured. - Run the script using Python:
python dns_updater.py
. - The script will fetch the current public IP addresses and update the Cloudflare DNS records if changes are detected.
- This script currently supports A, MX, NS record types and handles both IPv4 and IPv6 addresses.
This Dockerfile sets up an environment to run the DNSUpdater script in a containerized environment. The script automates DNS record updates for specific subdomains under Cloudflare.
- Base Image: Utilizes the latest Python image as the base image.
- Working Directory: Sets the working directory within the container to
/app
. - Copying Files: Copies necessary files (
DNSUpdater.py
,config.json
,requirements.txt
,crontab
) into the/app
directory. - Installing Dependencies: Updates
apt-get
, installscron
, installs Python packages fromrequirements.txt
, sets permissions for the crontab file, and sets up the cron job. - Running the Script: Executes the DNSUpdater script using Python after the setup is complete.
- Default Command: Sets the default command to keep the cron job running within the container.
To build the Docker image and run the container:
-
Place your
DNSUpdater.py
,config.json
,requirements.txt
, andcrontab
files in the same directory as this Dockerfile. -
Build the Docker image:
docker build -t dnsupdater .
-
Run the Docker container:
docker run -d --name dns_container dnsupdater
Ensure your config.json
file is properly configured with the required Cloudflare API key and domain details before building the Docker image.