Skip to content

jmrplens/Cloudflare-DNS-Updater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codacy Badge CodeFactor Shellcheck OpenSSF Scorecard OpenSSF Best Practices CircleCI

Dynamic DNS - Cloudflare

Bash script to update IPv4 and IPv6 records in Cloudflare. Update with WAN or LAN IP.

Based on this project: DDNS-Cloudflare-Bash.

Example

Result Settings
Screenshot of Termius (9-6-23, 01-13-49)
domains:
    - name: jmrp.dev
      ip_type: external
      ipv4: true
      ipv6: true
      proxied: true
      ttl: auto
    - name: git.jmrp.dev
      ip_type: external
      ipv4: true
      ipv6: true
      proxied: true
      ttl: auto
    - name: jenkins.jmrp.dev
      ip_type: external
      ipv4: true
      ipv6: true
      proxied: true
      ttl: auto

settings:
    cloudflare:
        - zone_id: #########
        - zone_api_token: ########
    misc:
        - create_if_no_exist: false

notifications:
    telegram: 
        enabled: false
        bot_token: token
        chat_id: id

About

  • Bash Script for most Linux, Unix distributions and MacOS.
  • Choose any source IP address to update external or internal (WAN/LAN) for ech domain.
  • For multiply lan interfaces like Wifi, Docker Networks and Bridges the script will automatically detects the primary Interface by priority.
  • Cloudflare's options proxy and TTL configurable via the config file for each domain.
  • Optional Telegram Notifications

Requirements

  • curl
  • Cloudflare api-token with ZONE-DNS-EDIT Permissions
  • DNS Record must be pre created in web interface (WIP: Create record if no exist)

Creating Cloudflare API Token

To create a CloudFlare API token for your DNS zone go to cloudflare-api-token-url and follow these steps:

  1. Click Create Token
  2. Select Create Custom Token
  3. Provide the token a name, for example, example.com-dns-zone-readonly
  4. Grant the token the following permissions:
    • Zone - DNS - Edit
  5. Set the zone resources to:
    • Include - Specific Zone - example.com
  6. Complete the wizard and use the generated token at the CLOUDFLARE_API_TOKEN variable for the container

Installation

You can place the script at any location manually.

MacOS: Don't use the /usr/local/bin/ for the script location. Create a separate folder under your user path /Users/${USER}

The automatic install examples below will place the script at /usr/local/bin/

wget https://raw.githubusercontent.com/jmrplens/DyDNS_Cloudflare_IPv4-6/main/update-cloudflare-records.sh
sudo chmod +x update-cloudflare-records.sh
sudo mv update-cloudflare-records.sh /usr/local/bin/update-cloudflare-records

Config file

You can use default config file update-cloudflare-records.yaml or pass your own config file as parameter to script.

wget https://raw.githubusercontent.com/jmrplens/DyDNS_Cloudflare_IPv4-6/main/update-cloudflare-records.yaml

Place the config file in the directory as the update-cloudflare-dns for above example at /usr/local/bin/

sudo mv update-cloudflare-records.yaml /usr/local/bin/update-cloudflare-records.yaml

Config Parameters

domains:
  - name: example.com
    ip_type: external
    ipv4: true
    ipv6: true
    proxied: true
    ttl: auto
    
settings:
  cloudflare:
    - zone_id: #########
    - zone_api_token: ########
  misc:
    - create_if_no_exist: false

notifications:
  telegram: 
    enabled: false
    bot_token: token
    chat_id: id

Multiple domains is supported:

domains:
  - name: example.com
    ip_type: external
    ipv4: true
    ipv6: true
    proxied: true
    ttl: auto
  - name: example2.com
    ip_type: external
    ipv4: true
    ipv6: true
    proxied: true
    ttl: auto
  - name: ..........
.........

Domains

Option Example Description
name example.com Domain name. Required
ip_type external Which IP should be used for the record: internal/external. Optional (default: external)
ipv4 true Update IPv4 DNS Record: true/false. Optional (default: true)
ipv6 true Update IPv6 DNS Record: true/false. Optional (default: true)
proxied true Use Cloudflare proxy on dns record: true/false. Optional (default: true)
ttl 3600 120-7200 in seconds or auto. Optional (default: auto)

Cloudflare

Option Example Description
zone_api_token token Cloudflare API Token KEEP IT PRIVATE!!!!
zone_id id Cloudflare's Zone ID
Cloudflare misc
Option Example Description
create_if_no_exist false Not yet implemented

Notifications

Telegram
Option Example Description
enabled true Use Telegram notifications: true/false.
bot_token token Telegram's Bot API Token
chat_id id Chat ID of the bot

Running The Script

When placed in /usr/local/bin/

update-cloudflare-records

With your config file (need to be placed in same folder)

update-cloudflare-records yoru_config.conf

Automation With Crontab

You can run the script via crontab

crontab -e

Examples

Example Code
Run every minute
* * * * * /usr/local/bin/update-cloudflare-records
Run every minute with your specific config file
* * * * * /usr/local/bin/update-cloudflare-records myconfig.yaml
Run every every 2 minutes
*/2 * * * * /usr/local/bin/update-cloudflare-records
Run at boot
@reboot /usr/local/bin/update-cloudflare-records
Run 1 minute after boot
@reboot sleep 60 && /usr/local/bin/update-cloudflare-records

Logs

This Script will create a log file with only the last run information Log file will be located at the script's location.

Example:

/usr/local/bin/update-cloudflare-records.log