18. Mikrotik Schedul+Script check WANs to internet



/system scheduler
add disabled=yes interval=5s name=ping_google_link_B on-event=scheduler-B \
    policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=jan/01/1970 start-time=00:00:00
add interval=5s name=ping_google_link_Both on-event=scheduler-Both policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=jan/01/1970 start-time=00:00:00
add disabled=yes interval=5s name=ping_google_link_A on-event=scheduler-A \
    policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=jan/01/1970 start-time=00:00:00

Script fro Schedule A:

#set variables
:local pingcount  3
:local ipA 192.168.89.74
:local ipB 192.168.73.74

#ping gateways with src
:local pingresultA [/ping 8.8.8.8 src-address=$ipA count=$pingcount]
:local pingresultB [/ping 8.8.8.8 src-address=$ipB count=$pingcount]

#if link_A is UP
:if (($pingresultA=3)) do={
#send email
/tool e-mail send subject=($ipA . " is up ") \
body=("the gateway is up now at: " . [/system clock get date]) \ to=("alertphalla@gmail.com") tls=yes

#enable scheduler-Both
/system scheduler set [find name=ping_google_link_Both] disabled=no
#disablescheduler-B
/system scheduler set [find name=ping_google_link_B] disabled=yes
#disable scheduler-A
/system scheduler set [find name=ping_google_link_A] disabled=yes

}

Script fro Schedule B:

#set variables
:local pingcount  3
:local ipA 192.168.89.74
:local ipB 192.168.73.74

#ping gateways with src
:local pingresultA [/ping 8.8.8.8 src-address=$ipA count=$pingcount]
:local pingresultB [/ping 8.8.8.8 src-address=$ipB count=$pingcount]

#if Link_B is UP
:if (($pingresultB=3)) do={
#send email
/tool e-mail send subject=($ipB . " is up ") \
body=("the gateway is up now at: " . [/system clock get date]) \ to=("alertphalla@gmail.com") tls=yes

#enable scheduler-Both
/system scheduler set [find name=ping_google_link_Both] disabled=no
#disable scheduler-A
/system scheduler set [find name=ping_google_link_A] disabled=yes
#disable scheduler-B
/system scheduler set [find name=ping_google_link_B] disabled=yes

}


Script fro Both Schedules:

#set variables
:local pingcount  3
:local ipA 192.168.89.74
:local ipB 192.168.73.74
:global state no

#ping gateways with src
:local pingresultA [/ping 8.8.8.8 src-address=$ipA count=$pingcount]
:local pingresultB [/ping 8.8.8.8 src-address=$ipB count=$pingcount]

#if link_A is DOWN and link_B is UP then:
:if (($pingresultA=0) && ($pingresultB>0)) do={
#send email
/tool e-mail send subject=($ipA . " is down") \
body=("the gateway is down now at: " . [/system clock get date]) \ to=("alertphalla@gmail.com") tls=yes

#disable scheduler-A
/system scheduler set [find name=ping_google_link_A] disabled=no
#disable scheduler-B
/system scheduler set [find name=ping_google_link_B] disabled=yes
#disable scheduler-Both
/system scheduler set [find name=ping_google_link_Both] disabled=yes
}

#if link_A is UP and link_B is DOWN then:
:if (($pingresultA>0) && ($pingresultB=0)) do={
#send email
/tool e-mail send subject=($ipB . " is down") \
body=("the gateway is down now at: " . [/system clock get date]) \ to=("alertphalla@gmail.com") tls=yes

#disable scheduler-B
/system scheduler set [find name=ping_google_link_B] disabled=no
#disable scheduler-A
/system scheduler set [find name=ping_google_link_A] disabled=yes
#disable scheduler-Both
/system scheduler set [find name=ping_google_link_Both] disabled=yes

}
Previous
Next Post »

4 comments

Write comments
yemencafe
AUTHOR
June 9, 2017 at 6:13 AM delete

nice work
but this script for check only
can you make script for check wan failover and disable in route and check every minutes if back and enable it again
that will be great
thank u

Reply
avatar
Unknown
AUTHOR
September 2, 2017 at 2:36 PM delete

Parabéns pela solução que achei fanstástica pra um FAILOVER bem configurado, porém, faltou um detalhe a ser mostrado, que é de adicionar 2 rotas forçando o ping para os DNSs desejados apontando para o gateway da WAN1 e WAN2 para que funcione de forma perfeita até mesmo quando uma interface WAN1 é desabilitada ou um cabo desconectado, digo porque, se ocorrer um destes incidentes, a rota padrão fica inativa e o teste de ping não irá falhar porque a rota do link secundário ficará ativa e o teste de ping não apresentará falha, ficando desta forma sem efeito o script.

Reply
avatar
Unknown
AUTHOR
June 28, 2018 at 8:49 PM delete

Hellow Concerns!

i have applied [18. Mikrotik Schedul+Script check WANs to internet] on my mikrotik having two wan with load balancing... but its not working... can you help me any more please...?

Reply
avatar
Unknown
AUTHOR
November 23, 2018 at 2:29 AM delete

hello.great job but it is'n working. can you help? what is the reason?

Reply
avatar