Technical Note: Automating Cloudflare DNS Updates (IPv4/IPv6) on MikroTik RouterOS 7

When using a dynamic IP at home or in the office, a DDNS (Dynamic DNS) script is essential to keep your domain pointing to the correct gateway. This guide covers how to retrieve Cloudflare API credentials and implement update scripts for both IPv4 and IPv6 on RouterOS 7 (ROS7).

  1. Prerequisites: Obtain Cloudflare Credentials

To authenticate your requests, you need to gather three specific identifiers from the Cloudflare Dashboard:
A. Zone ID

Log in to Cloudflare, select your domain, and find the Zone ID on the right-hand sidebar of the Overview page.

Example Zone ID: 06bd342b23635b0dabf6f13723070a240b

B. API Token

Navigate to My Profile > API Tokens and create a token using the “Edit Zone DNS” template.

Example Token: QVMFwVMfKuWZEEkrGF343Lu8RCwmUF4N02-8NVWbxq

C. DNS Record ID

Each DNS record (A or AAAA) has a unique ID. You can retrieve it by running the following curl command in your terminal:
Bash

curl -X GET “https://api.cloudflare.com/client/v4/zones/$YOUR_ZONE_ID/dns_records?name=$YOUR_DOMAIN” \
-H “Authorization: Bearer $YOUR_API_TOKEN” \
-H “Content-Type: application/json”

Find the “id” field in the JSON response corresponding to the record you want to update.

  1. Implementation in RouterOS 7

RouterOS 7’s /tool fetch supports HTTP methods like PATCH, making it ideal for Cloudflare’s V4 API.
IPv4 Update Script

This script fetches the current IP from a specific WAN interface and updates the Cloudflare A record.
代码段

—– Configuration —–

:local cfToken “QVMFwVMfKuWZEEkrGF343Lu8RCwmUF4N02-8NVWbxq”
:local cfZoneId “06bd342b23635b0dabf6f13723070a240b”
:local cfRecordId “64fa27f7e435ba7220775aa28683032db3”
:local cfDomain “www.yourdomain.xyz”
:local wanInterface “pppoe-out2” ;# Change this to your actual WAN interface name

—– Main Logic —–

1. Get current IP from the interface

:local currentIP [/ip address get [/ip address find interface=$wanInterface] address]
:set currentIP [:pick $currentIP 0 [:find $currentIP “/”]]

:log info “CF DDNS: Preparing to update $cfDomain. Current IP: $currentIP”

2. Execute PATCH request to Cloudflare API

/tool fetch http-method=patch \
mode=https \
url=”https://api.cloudflare.com/client/v4/zones/$cfZoneId/dns_records/$cfRecordId” \
http-header-field=”Authorization: Bearer $cfToken,Content-Type: application/json” \
http-data=”{\”content\”:\”$currentIP\”}” \
output=none

:log info “CF DDNS: IPv4 update request sent.”

IPv6 Update Script

For IPv6, the script extracts the prefix from the ND prefix table and matches the specific device’s suffix via the Neighbor table (using its MAC address).
代码段

—– Configuration —–

:local cfToken “QVMFwVMfKuWZEEkrGF343Lu8RCwmUF4N02-8NVWbxq”
:local cfZoneId “06bd342b23635b0dabf6f13723070a240b”
:local cfRecordId “f1174e74e5b2cb4b1240de8fbb325b10”
:local cfDomain “www.yourdomain.xyz”

—– IPv6 Extraction Logic —–

/ipv6/nd/prefix
:local targetID [find where interface=vlan53];

:if ($targetID != “”) do={
:local v6pre [get $targetID prefix];
:set v6pre [:pick $v6pre 0 ([:len $v6pre] – 5)];

/ipv6/neighbor
:local v6ip0 [get [find where address~$v6pre and (mac-address="00:50:56:A3:6A:4B")] address];

# ----- API Update Logic -----
:if ($v6ip0 != "") do={
    :log info "CF DDNS: Preparing to update AAAA record: $v6ip0"

    /tool fetch http-method=patch \
        mode=https \
        url="https://api.cloudflare.com/client/v4/zones/$cfZoneId/dns_records/$cfRecordId" \
        http-header-field="Authorization: Bearer $cfToken,Content-Type: application/json" \
        http-data="{\"content\":\"$v6ip0\",\"type\":\"AAAA\",\"name\":\"$cfDomain\"}" \
        output=none

    :log info "CF DDNS: IPv6 update request sent."
} else={
    :log error "CF DDNS: Failed to find matching IPv6 address for the specified MAC."
}

}

  1. Automation

To run these scripts automatically, add them to the System > Scheduler. An interval of 5 to 10 minutes is usually sufficient to ensure high availability without hitting API rate limits.

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部