本文介绍如何在RouterOS中使用脚本完成动态域名解析DDNS
1. Cloudflare DDNS 脚本
先祭出ddns脚本,仅提供解析至cf的脚本。自行准备好“你的DDNS token”“你的ZONEID””你的RECORD6ID“或”RECORD4ID“以及域名(支持ddns泛解析)。文末提供”ZONEID“、”RECORD4ID”等所需参数获取教程。
1.1 完整脚本模板
- 此模板为先更新IPV6记录,等待300s后更新IPV4记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
:local TOKEN "你的DDNS token"
:local ZONEID "你的ZONEID"
:local RECORD6ID "你的RECORD6ID"
:local RECORD6NAME "*.你的v6域名.com"
:local WANIF6 "pppoe-out1或bridge1"
:local url6 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD6ID/"
:local RECORD4ID "你的RECORD4ID"
:local RECORD4NAME "*.你的v4域名.com"
:local WANIF4 "pppoe-out1"
:local url4 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD4ID/"
:if ([/interface get $WANIF6 value-name=running]) do={
:local currentIP6 [/ipv6/address/get [:pick [find global interface=$WANIF6] 0 ] address]
:local IP6NEW [:pick $currentIP6 0 [:find $currentIP6 "/"]]
:log info "当前IPv6地址: $IP6NEW"
:local cfRecord [/tool fetch mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json"]
:local cfContent ($cfRecord->"data")
:local startIndex [:find $cfContent "\"content\":\""]
:local cfIP6 [:pick $cfContent ($startIndex + 11) [:find $cfContent "\"" ($startIndex + 11)]]
:log info "CF IPv6地址: $cfIP6"
:if ($IP6NEW != $cfIP6) do={
:log info "检测到 IP 变化:Cloudflare=$cfIP6, 本地=$IP6NEW"
:local cfUpdate6 [/tool fetch http-method=put mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
http-data="{\"type\":\"AAAA\",\"name\":\"$RECORD6NAME\",\"content\":\"$IP6NEW\",\"ttl\":120,\"proxied\":false}"]
:log info "已更新 Cloudflare IPv6 为 $IP6NEW"
} else={
:log info "当前 Cloudflare 记录 ($cfIP6) 与本机 IP 相同,无需更新。"
}
} else={
:log error "$WANIF6 接口未运行,无法获取本地 IPv6 地址"
}
:delay 300s
:if ([/interface get $WANIF4 value-name=running]) do={
:local currentIP4 [/ip/address get [/ip/address find interface=$WANIF4] address]
:local IP4NEW [:pick $currentIP4 0 [:find $currentIP4 "/"]]
:log info "当前IPv4地址: $IP4NEW"
:local cfRecord [/tool fetch mode=https url=$url4 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json"]
:local cfContent ($cfRecord->"data")
:local startIndex [:find $cfContent "\"content\":\""]
:local cfIP4 [:pick $cfContent ($startIndex + 11) [:find $cfContent "\"" ($startIndex + 11)]]
:log info "CF IPv4地址: $cfIP4"
:if ($IP4NEW != $cfIP4) do={
:log info "检测到 IP 变化:Cloudflare=$cfIP4, 本地=$IP4NEW"
:local cfUpdate4 [/tool fetch http-method=put mode=https url=$url4 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
http-data="{\"type\":\"A\",\"name\":\"$RECORD4NAME\",\"content\":\"$IP4NEW\",\"ttl\":120,\"proxied\":false}"]
:log info "已更新 Cloudflare IPv4 为 $IP4NEW"
} else={
:log info "当前 Cloudflare 记录 ($cfIP4) 与本机 IP 相同,无需更新。"
}
} else={
:log error "$WANIF4 接口未运行,无法获取本地 IPv4 地址"
}
|
1.2 单独 IPv4/IPv6 版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
:local TOKEN "你的DDNS token"
:local ZONEID "你的ZONEID"
:local RECORD6ID "你的RECORD6ID"
:local RECORD6NAME "*.你的v6域名.com"
:local WANIF6 "pppoe-out1或是bridge1"
:local url6 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD6ID/"
:if ([/interface get $WANIF6 value-name=running]) do={
:local currentIP6 [/ipv6/address/get [:pick [find global interface=$WANIF6] 0 ] address]
:local IP6NEW [:pick $currentIP6 0 [:find $currentIP6 "/"]]
:log info "当前IPv6地址: $IP6NEW"
:local cfRecord [/tool fetch mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json"]
:local cfContent ($cfRecord->"data")
:local startIndex [:find $cfContent "\"content\":\""]
:local cfIP6 [:pick $cfContent ($startIndex + 11) [:find $cfContent "\"" ($startIndex + 11)]]
:log info "CF IPv6地址: $cfIP6"
:if ($IP6NEW != $cfIP6) do={
:log info "检测到 IP 变化:Cloudflare=$cfIP6, 本地=$IP6NEW"
:local cfUpdate6 [/tool fetch http-method=put mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
http-data="{\"type\":\"AAAA\",\"name\":\"$RECORD6NAME\",\"content\":\"$IP6NEW\",\"ttl\":120,\"proxied\":false}"]
:log info "已更新 Cloudflare IPv6 为 $IP6NEW"
} else={
:log info "当前 Cloudflare 记录 ($cfIP6) 与本机 IP 相同,无需更新。"
}
} else={
:log error "$WANIF6 接口未运行,无法获取本地 IPv6 地址"
}
|
2. 在 RouterOS 中配置 DDNS
- 配置DNS static (可选项,博主所在地区无法直连cf的api,所以设置后通过代理访问cf的api)
1
| /ip dns static add address=104.19.192.175 name=api.cloudflare.com type=A
|
- 添加脚本,名字随意

- 点击运行脚本,查看log是否报错

- 若报错,将104.19.192.175路由至sing-box走代理

- 添加计划任务,3分钟执行一次

3. 获取 DDNS 脚本所需参数
3.1 获取 Global API Key
- 登录CF主页,点击我的个人资料

- 输入密码查看Global API KEY


- 创建DDNS token



注:API令牌仅出现1次,务必保存好

3.2 获取 ZONEID
- 账户主页-进入需DDNS的域名界面

- 往下滑找到区域ID即为ZONEID

3.3 获取 RECORDID
若域名无DNS记录,需先创建1条DNS记录
在任意linux系统中通过下述命令获取你的RECORDID,A代表V4,AAAA代表V6,如果同域名需要解析V4和V6,在CF上使用同一个二个域名,分别选择A和AAAA
1 2 3 4
| curl -X GET "https://api.cloudflare.com/client/v4/zones/这里输入你的ZONEID/dns_records?type=A&name=你的域名" \ -H "X-Auth-Email: 这里输入你的CF帐号邮箱" \ -H "X-Auth-Key: 这里输入你的global_TOKEN" \ -H "Content-Type: application/json"
|
1 2 3 4
| curl -X GET "https://api.cloudflare.com/client/v4/zones/这里输入你的ZONEID/dns_records?type=AAAA&name=你的域名" \ -H "X-Auth-Email: 这里输入你的CF帐号邮箱" \ -H "X-Auth-Key: 这里输入你的global_TOKEN" \ -H "Content-Type: application/json"
|