Community discussions

MikroTik App
 
Skream
just joined
Topic Author
Posts: 24
Joined: Mon Jun 12, 2017 10:48 am

4 WAN - Automatically changes WAN

Mon Jun 12, 2017 11:18 am

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.
 
AldoJimenez
Trainer
Trainer
Posts: 30
Joined: Fri Feb 12, 2016 11:49 pm
Contact:

Re: 4 WAN - Automatically changes WAN

Mon Jun 12, 2017 4:18 pm

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.
 
Skream
just joined
Topic Author
Posts: 24
Joined: Mon Jun 12, 2017 10:48 am

Re: 4 WAN - Automatically changes WAN

Wed Jun 21, 2017 7:50 pm

Okey, thanks, but i need to check internet and not gateway, an idea please ?
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: 4 WAN - Automatically changes WAN

Wed Jun 21, 2017 9:31 pm

 
Skream
just joined
Topic Author
Posts: 24
Joined: Mon Jun 12, 2017 10:48 am

Re: 4 WAN - Automatically changes WAN

Thu Jun 22, 2017 3:54 pm

I go try this thanks ;)
 
Skream
just joined
Topic Author
Posts: 24
Joined: Mon Jun 12, 2017 10:48 am

Re: 4 WAN - Automatically changes WAN

Fri Jun 23, 2017 6:12 pm

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
}
 
christianrodher
just joined
Posts: 17
Joined: Thu Apr 09, 2015 9:26 pm

Re: 4 WAN - Automatically changes WAN

Mon Apr 30, 2018 1:46 am

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.
 
erikhp
Trainer
Trainer
Posts: 7
Joined: Sat Aug 22, 2009 2:12 am

Re: 4 WAN - Automatically changes WAN

Tue Aug 28, 2018 8:49 am

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!!!
 
angboontiong
Forum Guru
Forum Guru
Posts: 1136
Joined: Fri Jan 16, 2009 9:59 am

Re: 4 WAN - Automatically changes WAN

Sun Apr 28, 2019 4:21 pm

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.
 
Frostbyte
Frequent Visitor
Frequent Visitor
Posts: 92
Joined: Mon Dec 25, 2017 1:42 am

Re: 4 WAN - Automatically changes WAN

Mon Apr 29, 2019 2:29 am

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. :)

Who is online

Users browsing this forum: No registered users and 39 guests