You are on page 1of 1

/system scheduler

add interval=2m30s name="Enable/Disable Routes" on-event="Enable/Disable Routes"


policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive start-
time=startup

/system script
add name="Enable/Disable Routes" owner=admin
policy=reboot,read,write,policy,test,password,sniff,sensitive source="#set
variables\r\
\n:local pingcount 3\r\
\n:local pingip [:resolve \"www.google.com\"]\r\
\n:local Gateways \"192.168.2.1,192.168.100.1,192.168.4.1\"\r\
\n:local Gateway [:toarray \$Gateways]\r\
\n\r\
\n#Setup Log File\r\
\n:if ([/system logging action print count-only where name=GatewaysCheck]=0)
do={/system logging action add name=GatewaysCheck target=disk disk-file-
name=\"Gateways Check\" disk\
-lines-per-file=10000}\r\
\n:if ([/system logging print count-only where action=GatewaysCheck]=0)
do={/system logging add topics=script action=GatewaysCheck}\r\
\n\r\
\n:if ([/ip route print count-only where dst-address=\"\$pingip/32\"]=0)
do={/ip route add dst-address=(\$pingip) gateway=(192.168.100.254)
comment=\"Gateway Check\"};\r\
\n\r\
\n:foreach k in \$Gateway do={\r\
\n#Test Gateways:\r\
\n/ip route set [find dst-address=\"\$pingip/32\"] disabled=no gateway=\$k
comment=\"Checking Gateway \$k ...\";\r\
\n:delay 1000ms;\r\
\n:if ([/ip route get [find dst-address=\"\$pingip/32\"] gateway-status] = \"\
$k unreachable\") do={:log info (\"Router \$k not present or unconfigured\")}
else={\r\
\n:local pingresult [/ping \$pingip count=\$pingcount];\r\
\n# Gateway enable/disable:\r\
\n:if (\$pingresult=0) do={:foreach i in=[/ip route find gateway=\$k] do={/ip
route set \$i disabled=yes}};\r\
\n:if (\$pingresult>0) do={:foreach i in=[/ip route find gateway=\$k] do={:if
([/ip route get \$i disabled]) do={/ip route set \$i disabled=no}}};\r\
\n:if (\$pingresult=0) do={:log info (\"Gateway \$k Down! \$pingresult / \
$pingcount\")} else={:log info (\"Gateway \$k Up \$pingresult / \$pingcount\")};\r\
\n:delay 3000ms;\r\
\n}\r\
\n}\r\
\n/ip route remove [find dst-address=\"\$pingip/32\"]"

You might also like