⛔ As rotas default devem ser estáticas e devem ser inseridos comentários.
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 |
# Remove regras de roteamento e scripts antigos /ip route remove [find comment~"!=="] /ip route rule remove [find comment~"!=="] /system script remove [find name=failover] /system scheduler remove [find name="executa script failover"] # Cria rotas de teste para cada rota existente :foreach i in=[/ip route find dst-address=0.0.0.0/0 routing-mark<>""] do={ :global gateway [/ip route get number=$i gateway] :global comentario [/ip route get number=$i comment] /ip route add gateway=$gateway routing-mark=("rota-de-teste-para-" . $comentario) comment=("!== Rota de teste para " . $comentario) /ip route rule add action=lookup-only-in-table routing-mark=("rota-de-teste-para-" . $comentario) table=("rota-de-teste-para-" . $comentario) comment=("!== Force teste para o failover ==> ".$comentario) } # Cria o agendador para executar o script failover /system scheduler add interval=30s name="executa script failover" on-event=failover policy=\ ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \ start-time=startup # Script failover /system script add name=failover owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=" # IPs a serem testados separados por vírgula # Intervalo = timeout de cada ping (500ms) :local IPsParaTeste \"8.8.8.8,8.8.4.4,1.1.1.1,208.67.222.222\" :local NumeroDePings 2 :local Intervalo 500ms # Converte strings em arrays :local IPsParaTesteArray [:toarray \$IPsParaTeste] # Para cada rota de teste existente :foreach i in=[/ip route find dst-address=0.0.0.0/0 routing-mark<>\"\"] do={ :local comentario [/ip route get number=\$i comment] :local TabRoteAtual (\"rota-de-teste-para-\" . \$comentario) { :local LinkUP 0 # Testa a conectividade com cada IP de teste :foreach IPAtualParaTeste in=\$IPsParaTesteArray do= { :set LinkUP (\$LinkUP + [ping \$IPAtualParaTeste interval=\$Intervalo count=\$NumeroDePings routing-table=\$TabRoteAtual]) } # Ativa ou desativa as rotas de acordo com o estado do link :if (\$LinkUP=0) do={ :if ([/ip route print count-only where comment=\$comentario disabled]=0) do={ /ip route disable [find comment=\$comentario] } } else={ :if ([/ip route print count-only where comment=\$comentario disabled]>0) do={ /ip route enable [find comment=\$comentario] } } } } " |
—
📚 REFERÊNCIAS
- BOWDEN, Nigel. MikroTik Scripting: Unleash the Power of RouterOS Task Automation.
- STATO FILHO, André. Domine O Mikrotik Routeros Na Prática – do Básico as Certificações MTCNA e MTCRE – Passo A Passo. 2ª Edição.
- RODRIGUES, Marcio. Basico Mikrotik: Monte seu Provedor.
*Atualizado em 13 de julho de 2023