RouterOS 自动 DNS 切换脚本 - 保证网络连接
以下是一个使用 RouterOS 命令行工具的示例脚本,用于实现自动 DNS 切换,确保网络连接畅通:
:local primaryDNS '10.0.0.1'
:local secondaryDNS '223.5.5.5,119.29.29.29,223.6.6.6'
:if ([ping count=5 address=8.8.8.8]=5) do={
:if ([:typeof [:resolve 'google.com']])!='nil') do={
:if ([:resolve 'google.com']!=$primaryDNS) do={
/ip dns set servers=$primaryDNS
}
}
} else={
:local currentDNS [/ip dns get servers]
:if (($currentDNS!=$secondaryDNS) && ($currentDNS!='')) do={
/ip dns set servers=$secondaryDNS
}
}
该脚本首先尝试 ping 8.8.8.8 来判断网络连接状态。如果连接正常,则会尝试解析 'google.com' 来判断当前 DNS 服务器是否为 10.0.0.1。如果不是,则会将 DNS 服务器设置为 10.0.0.1。
如果 ping 8.8.8.8 失败,则会判断当前 DNS 服务器是否为 223.5.5.5, 119.29.29.29, 223.6.6.6。如果不是,则会将 DNS 服务器设置为 223.5.5.5, 119.29.29.29, 223.6.6.6。
请注意,上述脚本假设您的 RouterOS 设备已经正确配置了 DNS 服务器地址。
原文地址: https://www.cveoy.top/t/topic/bAco 著作权归作者所有。请勿转载和采集!