I figure someone would be in need of something similar so might as well post it for someone. Change the IPs and netwatch numbers to match your environments obviously.
Potential pitfalls is the use of numbers getting netwatch info.
Code: Select all
# Set our netwatches on our DNS servers
/tool netwatch
add disabled=no down-script="/system script run reset-dns" host=10.0.0.1 interval=2s timeout=1s up-script="/system script run reset-dns"
add disabled=no down-script="/system script run reset-dns" host=10.0.0.2 interval=2s timeout=1s up-script="/system script run reset-dns"
add disabled=no down-script="/system script run reset-dns" host=10.0.0.3 interval=2s timeout=1s up-script="/system script run reset-dns"
# Set our script to check the netwatch status's and set the dns servers to what is up
/system script
add name=reset-dns policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive source=":local dnsservers;\r\
\n\r\
\nif ([/tool netwatch get value-name=status number=0] = \"up\") do={\r\
\n :if ([:len \$dnsservers] = 0) do={\r\
\n :set dnsservers \"10.0.0.1\";\r\
\n } else={\r\
\n :set dnsservers \"\$dnsservers,10.0.0.1\";\r\
\n }\r\
\n}\r\
\n\r\
\nif ([/tool netwatch get value-name=status number=1] = \"up\") do={\r\
\n :if ([:len \$dnsservers] = 0) do={\r\
\n :set dnsservers \"10.0.0.2\";\r\
\n } else={\r\
\n :set dnsservers \"\$dnsservers,10.0.0.2\";\r\
\n }\r\
\n}\r\
\nif ([/tool netwatch get value-name=status number=2] = \"up\") do={\r\
\n :if ([:len \$dnsservers] = 0) do={\r\
\n :set dnsservers \"10.0.0.3\";\r\
\n } else={\r\
\n :set dnsservers \"\$dnsservers,10.0.0.3\";\r\
\n }\r\
\n}\r\
\n\r\
\n:if ([:len \$dnsservers] = 0) do={\r\
\n :set dnsservers \"8.8.8.8,8.8.4.4\";\r\
\n}\r\
\n/ip dns set servers=\"\$dnsservers\";\r\
\n:log info (\"changed dns servers to: \$dnsservers\");"
Code: Select all
:local dnsservers;
if ([/tool netwatch get value-name=status number=0] = "up") do={
:if ([:len $dnsservers] = 0) do={
:set dnsservers "10.0.0.1";
} else={
:set dnsservers "$dnsservers,10.0.0.1";
}
}
if ([/tool netwatch get value-name=status number=1] = "up") do={
:if ([:len $dnsservers] = 0) do={
:set dnsservers "10.0.0.2";
} else={
:set dnsservers "$dnsservers,10.0.0.2";
}
}
if ([/tool netwatch get value-name=status number=2] = "up") do={
:if ([:len $dnsservers] = 0) do={
:set dnsservers "10.0.0.3";
} else={
:set dnsservers "$dnsservers,10.0.0.3";
}
}
:if ([:len $dnsservers] = 0) do={
:set dnsservers "8.8.8.8,8.8.4.4";
}
/ip dns set servers="$dnsservers";
:log info ("changed dns servers to: $dnsservers");