Page 1 of 1
4 WAN - Automatically changes WAN
Posted: Mon Jun 12, 2017 11:18 am
by Skream
Hello,
It's my first time on a Mikrotik.
I have 4 WAN. (WAN1 - WAN2 - WAN3 - WAN4)
I need a script, when WAN1 is timeout, WAN2 takes over.
When WAN2 is timeout, WAN3 takes over, ....
Please help
Thanks you.
Re: 4 WAN - Automatically changes WAN
Posted: Mon Jun 12, 2017 4:18 pm
by AldoJimenez
You can use something similar to the script below replacing the WANX for the IP's.
/ip route add dst-address=0.0.0.0/0 gateway=WAN1 distance=1 check-gateway=ping
/ip route add dst-address=0.0.0.0/0 gateway=WAN2 distance=2 check-gateway=ping
/ip route add dst-address=0.0.0.0/0 gateway=WAN3 distance=3 check-gateway=ping
/ip route add dst-address=0.0.0.0/0 gateway=WAN4 distance=4 check-gateway=ping
You will need src-nat (masquerade) rules for each one.
Re: 4 WAN - Automatically changes WAN
Posted: Wed Jun 21, 2017 7:50 pm
by Skream
Okey, thanks, but i need to check internet and not gateway, an idea please ?
Re: 4 WAN - Automatically changes WAN
Posted: Wed Jun 21, 2017 9:31 pm
by nescafe2002
Re: 4 WAN - Automatically changes WAN
Posted: Thu Jun 22, 2017 3:54 pm
by Skream
I go try this thanks
Re: 4 WAN - Automatically changes WAN
Posted: Fri Jun 23, 2017 6:12 pm
by Skream
Your solution does not work the way I want.
I have for example 2 WAN, and I need a solution to automatically change from WAN1 to WAN2 if WAN1 no longer has access to the internet, not just disconnected.
So it must check if the internet always passes via a ping if it does not work it automatically changes interface (WAN1 to WAN2) and as soon as WAN1 to new internet it gets over it.
So I decided to try with a script. Except that it is not working properly. It does not change the distance and change the interface.
A solution for my problem ?
Thanks you !
My Script :
Code: Select all
:global MainIf ether1
:global RsrvIf ether2
:local PingCount 3
:local PingTarget1 8.8.8.8
:local PingTarget2 91.219.24.37
:local PingTarget3 213.59.5.110
:local PingTarget1R 8.8.8.8
:local PingTarget2R 80.68.243.243
:local PingTarget3R 8.8.4.4
:local MainIfInetOk false;
:local PingResult1 [/ping $PingTarget1 count=$PingCount interface=$MainIf]
:local PingResult2 [/ping $PingTarget2 count=$PingCount interface=$MainIf]
:local PingResult3 [/ping $PingTarget3 count=$PingCount interface=$MainIf]
:set MainIfInetOk (($PingResult1 + $PingResult2 + $PingResult3) >= (2 * $PingCount))
:local RsrvIfInetOk false;
:local PingResult1 [/ping $PingTarget1R count=$PingCount interface=$RsrvIf]
:local PingResult2 [/ping $PingTarget2R count=$PingCount interface=$RsrvIf]
:local PingResult3 [/ping $PingTarget3R count=$PingCount interface=$RsrvIf]
:set RsrvIfInetOk (($PingResult1 + $PingResult2 + $PingResult3) >= (2 * $PingCount))
:put "MainIfInetOk=$MainIfInetOk"
:put "RsrvIfInetOk=$RsrvIfInetOk"
if (!$MainIfInetOk) do={
/log error "Main internet connection error"
}
if (!$RsrvIfInetOk) do={
/log error "Reserve internet connection error"
}
:local MainGWDistance [/ip route get [find comment="Prov1"] distance]
:local RsrvGWDistance [/ip route get [find comment="Prov2"] distance]
:put "MainGWDistance=$MainGWDistance"
:put "RsrvGWDistance=$RsrvGWDistance"
#SetUp gateways
if ($MainIfInetOk && ($MainGWDistance >= $RsrvGWDistance)) do={
/ip route set [find comment="Prov1"] distance=1
/ip route set [find comment="Prov2"] distance=2
/interface disable ether2
/ip firewall connection tracking set enabled=no
delay 5
/ip firewall connection tracking set enabled=yes
/ip ipsec installed-sa flush sa-type=all
/log warning "Switch to main internet connection"
/interface enable ether2
}
if (!$MainIfInetOk && $RsrvIfInetOk && ($MainGWDistance <= $RsrvGWDistance)) do={
/ip route set [find comment="Prov1"] distance=2
/ip route set [find comment="Prov2"] distance=1
/interface disable ether1
/ip firewall connection tracking set enabled=no
delay 5
/ip firewall connection tracking set enabled=yes
/ip ipsec installed-sa flush sa-type=all
/log warning "Switch to reserve internet connection"
/interface enable ether1
}
Re: 4 WAN - Automatically changes WAN
Posted: Mon Apr 30, 2018 1:46 am
by christianrodher
Hello,
It's my first time on a Mikrotik.
I have 4 WAN. (WAN1 - WAN2 - WAN3 - WAN4)
I need a script, when WAN1 is timeout, WAN2 takes over.
When WAN2 is timeout, WAN3 takes over, ....
Please help
Thanks you.
did you find a solution? Im looking for the same exact thing.
Re: 4 WAN - Automatically changes WAN
Posted: Tue Aug 28, 2018 8:49 am
by erikhp
Hello guys, you can achieve what you are looking using check-gateway and recursive gateway, you can add multiple WANs and monitor as much sites (hosts) per WAN as you need. All of this without scripting:
https://wiki.mikrotik.com/wiki/Advanced ... _Scripting
Enjoy!!!
Re: 4 WAN - Automatically changes WAN
Posted: Sun Apr 28, 2019 4:21 pm
by angboontiong
Your solution does not work the way I want.
I have for example 2 WAN, and I need a solution to automatically change from WAN1 to WAN2 if WAN1 no longer has access to the internet, not just disconnected.
So it must check if the internet always passes via a ping if it does not work it automatically changes interface (WAN1 to WAN2) and as soon as WAN1 to new internet it gets over it.
So I decided to try with a script. Except that it is not working properly. It does not change the distance and change the interface.
A solution for my problem ?
Thanks you !
My Script :
Code: Select all
:global MainIf ether1
:global RsrvIf ether2
:local PingCount 3
:local PingTarget1 8.8.8.8
:local PingTarget2 91.219.24.37
:local PingTarget3 213.59.5.110
:local PingTarget1R 8.8.8.8
:local PingTarget2R 80.68.243.243
:local PingTarget3R 8.8.4.4
:local MainIfInetOk false;
:local PingResult1 [/ping $PingTarget1 count=$PingCount interface=$MainIf]
:local PingResult2 [/ping $PingTarget2 count=$PingCount interface=$MainIf]
:local PingResult3 [/ping $PingTarget3 count=$PingCount interface=$MainIf]
:set MainIfInetOk (($PingResult1 + $PingResult2 + $PingResult3) >= (2 * $PingCount))
:local RsrvIfInetOk false;
:local PingResult1 [/ping $PingTarget1R count=$PingCount interface=$RsrvIf]
:local PingResult2 [/ping $PingTarget2R count=$PingCount interface=$RsrvIf]
:local PingResult3 [/ping $PingTarget3R count=$PingCount interface=$RsrvIf]
:set RsrvIfInetOk (($PingResult1 + $PingResult2 + $PingResult3) >= (2 * $PingCount))
:put "MainIfInetOk=$MainIfInetOk"
:put "RsrvIfInetOk=$RsrvIfInetOk"
if (!$MainIfInetOk) do={
/log error "Main internet connection error"
}
if (!$RsrvIfInetOk) do={
/log error "Reserve internet connection error"
}
:local MainGWDistance [/ip route get [find comment="Prov1"] distance]
:local RsrvGWDistance [/ip route get [find comment="Prov2"] distance]
:put "MainGWDistance=$MainGWDistance"
:put "RsrvGWDistance=$RsrvGWDistance"
#SetUp gateways
if ($MainIfInetOk && ($MainGWDistance >= $RsrvGWDistance)) do={
/ip route set [find comment="Prov1"] distance=1
/ip route set [find comment="Prov2"] distance=2
/interface disable ether2
/ip firewall connection tracking set enabled=no
delay 5
/ip firewall connection tracking set enabled=yes
/ip ipsec installed-sa flush sa-type=all
/log warning "Switch to main internet connection"
/interface enable ether2
}
if (!$MainIfInetOk && $RsrvIfInetOk && ($MainGWDistance <= $RsrvGWDistance)) do={
/ip route set [find comment="Prov1"] distance=2
/ip route set [find comment="Prov2"] distance=1
/interface disable ether1
/ip firewall connection tracking set enabled=no
delay 5
/ip firewall connection tracking set enabled=yes
/ip ipsec installed-sa flush sa-type=all
/log warning "Switch to reserve internet connection"
/interface enable ether1
}
Hi dude, you make it?
i also looking for same thing.
Re: 4 WAN - Automatically changes WAN
Posted: Mon Apr 29, 2019 2:29 am
by Frostbyte
You may check my signature for a suite of scripts which includes a fail-over mechanism for N WAN gateways, plus reads the configuration from a file.