RouterOS(以下简称ROS)的原生脚本来实现域名的动态解析。
版本说明
1、该脚本只在RouterOS v7.x测试通过,其他版本Ros请自行测试。
2、不存在的记录,不会自动添加,需要手动在cloudflare添加记录,默认了解析A记录,TTL=60秒,不开启代理模式。为什么不加自动了添加域名之类的,因为一次添加永久有效,所以脚本中不添加自动了。节省ROS性能(我懒)
3、只支持IPv4,不支持IPv6
4、脚本中需要自己修改的信息
Token令牌脚本代码
#PPPoE :local pppoe "宽带拨号名称" #cloudflare :local token "cloudflare令牌" #域名 :local record "@" :local domain "77bx.com" #以下非专业人士请勿修改 #获取本地IPv4地址 :global ipold :local ipnew [/ip address get [/ip address find interface=$pppoe] address] :set ipnew [:pick $ipnew 0 ([len $ipnew] -3)] #本地判断IP变化 :if ($ipold != $ipnew) do={ #获取zoneid :local zoneid :local result [/tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/\?name=$domain" mode=https http-header-field="Content-Type: application/json,Authorization:Bearer $token" http-data="" as-value output=user] :local response ($result->"data") :if ([:find $response "\"id\":\""] > 0) do={ :set zoneid [:pick $response ([:find $response "\"id\":\""] + 6) [:find $response "\",\"" [:find $response "\"id\":\""]]] } #获取recordid :local recordid :local name :if ($record != "@") do={ :set name ($record.".".$domain) } else={ :set name $domain } :local result [/tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/\?type=A&name=$name" mode=https http-header-field="Content-Type: application/json,Authorization:Bearer $token" http-data="" as-value output=user] :local response ($result->"data") :if ([:find $response "\"id\":\""] > 0) do={ :set recordid [:pick $response ([:find $response "\"id\":\""] + 6) [:find $response "\",\"" [:find $response "\"id\":\""]]] } :local content :if ([:find $response "\"content\":\""] > 0) do={ :set content [:pick $response ([:find $response "\"content\":\""] + 11) [:find $response "\"" ([:find $response "\"content\":\""] + 11)]] } #远程判断IP变化 :if ($content != $ipnew) do={ #更新记录 :local result [/tool fetch http-method=put url="https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$recordid" mode=https http-header-field="Content-Type: application/json,Authorization:Bearer $token" http-data="{\"type\":\"A\",\"name\":\"$name\",\"content\":\"$ipnew\",\"ttl\":60,\"proxied\":false}" as-value output=user] :local response ($result->"data") :if ([:find $response "\"success\":true"] > 0) do={ :set ipold $ipnew :log info "DDNS: changed $ipold to $ipnew" } else={ :log warning "DDNS update error" } } else={ :set ipold $ipnew } }
修改以上参数
pppoe:宽带拨号名称
token:cloudflare API令牌
record:主机记录
domain:域名
API全局key脚本代码
#PPPoE :local pppoe "宽带拨号名称" #cloudflare :local email "cloudflare账号邮箱" :local key "cloudflare Global API key" #域名 :local record "@" :local domain "77bx.com" #以下非专业人士请勿修改 #获取本地IPv4地址 :global ipold :local ipnew [/ip address get [/ip address find interface=$pppoe] address] :set ipnew [:pick $ipnew 0 ([len $ipnew] -3)] #本地判断IP变化 :if ($ipold != $ipnew) do={ #获取zoneid :local zoneid :local result [/tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/\?name=$domain" mode=https http-header-field="Content-Type: application/json,X-Auth-Email:$email,X-Auth-Key:$key" http-data="" as-value output=user] :local response ($result->"data") :if ([:find $response "\"id\":\""] > 0) do={ :set zoneid [:pick $response ([:find $response "\"id\":\""] + 6) [:find $response "\",\"" [:find $response "\"id\":\""]]] } #获取recordid :local recordid :local name :if ($record != "@") do={ :set name ($record.".".$domain) } else={ :set name $domain } :local result [/tool fetch http-method=get url="https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/\?type=A&name=$name" mode=https http-header-field="Content-Type: application/json,X-Auth-Email:$email,X-Auth-Key:$key" http-data="" as-value output=user] :local response ($result->"data") :if ([:find $response "\"id\":\""] > 0) do={ :set recordid [:pick $response ([:find $response "\"id\":\""] + 6) [:find $response "\",\"" [:find $response "\"id\":\""]]] } :local content :if ([:find $response "\"content\":\""] > 0) do={ :set content [:pick $response ([:find $response "\"content\":\""] + 11) [:find $response "\"" ([:find $response "\"content\":\""] + 11)]] } #远程判断IP变化 :if ($content != $ipnew) do={ #更新记录 :local result [/tool fetch http-method=put url="https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$recordid" mode=https http-header-field="Content-Type: application/json,X-Auth-Email:$email,X-Auth-Key:$key" http-data="{\"type\":\"A\",\"name\":\"$name\",\"content\":\"$ipnew\",\"ttl\":60,\"proxied\":false}" as-value output=user] :local response ($result->"data") :if ([:find $response "\"success\":true"] > 0) do={ :set ipold $ipnew :log info "DDNS: changed $ipold to $ipnew" } else={ :log warning "DDNS update error" } } else={ :set ipold $ipnew } }
修改以上参数
pppoe:宽带拨号名称
email:cloudflare账号邮箱
key:cloudflare Global API key
record:主机记录
domain:域名
目前有 0 条评论