06.RouterOS配置ddns脚本

本文介绍如何在RouterOS中使用脚本完成动态域名解析DDNS

先祭出ddns脚本,仅提供解析至cf的脚本。自行准备好“你的DDNS token”“你的ZONEID””你的RECORD6ID“或”RECORD4ID“以及域名(支持ddns泛解析)。文末提供”ZONEID“、”RECORD4ID”等所需参数获取教程。

  • 此模板为先更新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
# token $ zoneid

:local TOKEN "你的DDNS token"

:local ZONEID "你的ZONEID"

# IPv6

:local RECORD6ID "你的RECORD6ID"

:local RECORD6NAME "*.你的v6域名.com"

:local WANIF6 "pppoe-out1或bridge1" #自行更改,NAT6选PPPOE接口,SLAAC选bridge

:local url6 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD6ID/"

# IPv4

:local RECORD4ID "你的RECORD4ID"

:local RECORD4NAME "*.你的v4域名.com"

:local WANIF4 "pppoe-out1" #自行更改

:local url4 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD4ID/"

# 更新 IPv6 记录

:if ([/interface get $WANIF6 value-name=running]) do={

# 获取本地 IPv6 地址

:local currentIP6 [/ipv6/address/get [:pick [find global interface=$WANIF6] 0 ] address]

:local IP6NEW [:pick $currentIP6 0 [:find $currentIP6 "/"]]

:log info "当前IPv6地址: $IP6NEW"

# 获取 Cloudflare 当前的 DNS 记录

: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"]



# 解析 Cloudflare 返回的 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"



# 对比本机 IP 和 Cloudflare 记录

:if ($IP6NEW != $cfIP6) do={

:log info "检测到 IP 变化:Cloudflare=$cfIP6, 本地=$IP6NEW"

# 更新 Cloudflare 记录

: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 地址"

}

#等待300s,若仅更新V4或V6需删除

:delay 300s

# 更新 IPv4 记录

:if ([/interface get $WANIF4 value-name=running]) do={

# 获取本地 IPv4 地址

:local currentIP4 [/ip/address get [/ip/address find interface=$WANIF4] address]

:local IP4NEW [:pick $currentIP4 0 [:find $currentIP4 "/"]]

:log info "当前IPv4地址: $IP4NEW"



# 获取 Cloudflare 当前的 DNS 记录

: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"]



# 解析 Cloudflare 返回的 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"



# 对比本机 IP 和 Cloudflare 记录

:if ($IP4NEW != $cfIP4) do={

:log info "检测到 IP 变化:Cloudflare=$cfIP4, 本地=$IP4NEW"

# 更新 Cloudflare 记录

: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 地址"

}
  • 若仅需更新IPV4或IPV6,删除多余板块和”:delay 300s”即可

  • 以仅更新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
# token $ zoneid

:local TOKEN "你的DDNS token"

:local ZONEID "你的ZONEID"

# IPv6

:local RECORD6ID "你的RECORD6ID"

:local RECORD6NAME "*.你的v6域名.com"

:local WANIF6 "pppoe-out1或是bridge1" #自行更改,NAT6选PPPOE接口,SLAAC选bridge

:local url6 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD6ID/"

# 更新 IPv6 记录

:if ([/interface get $WANIF6 value-name=running]) do={

# 获取本地 IPv6 地址

:local currentIP6 [/ipv6/address/get [:pick [find global interface=$WANIF6] 0 ] address]

:local IP6NEW [:pick $currentIP6 0 [:find $currentIP6 "/"]]

:log info "当前IPv6地址: $IP6NEW"

# 获取 Cloudflare 当前的 DNS 记录

: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"]



# 解析 Cloudflare 返回的 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"



# 对比本机 IP 和 Cloudflare 记录

:if ($IP6NEW != $cfIP6) do={

:log info "检测到 IP 变化:Cloudflare=$cfIP6, 本地=$IP6NEW"

# 更新 Cloudflare 记录

: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 地址"

}

ROS中配置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分钟执行一次

获取DDNS脚本所需参数

获取Global API Key

  • 登录CF主页,点击我的个人资料
  • 输入密码查看Global API KEY

  • 创建DDNS token



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

获取ZONEID

  • 账户主页-进入需DDNS的域名界面
  • 往下滑找到区域ID即为ZONEID
  • 获取RECORDID
    若域名无DNS记录,需先创建1条DNS记录
    在任意linux系统中通过下述命令获取你的RECORDID,A代表V4,AAAA代表V6,如果同域名需要解析V4和V6,在CF上使用同一个二个域名,分别选择A和AAAA
  • 获取RECORD4ID(IPV4)
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"
  • 获取RECORD6ID(IPV6)
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"