Dual WAN Failover Script Ping Command

Hello

I have a setup with Dual WAN. I would like to replicate the current EdgeRouter based setup. The current EdgeRouter Setup pings one IP on the Internet from both Interfaces. It automatically creates two internal Routing Tables with one or the other Interfaces Gateway as Default Gateway. It then uses these two internal Routing Tables to run Pings for one or the other WAN Interface.

My way to solve this now would be to write a Mikrotik Script which on every run does housekeeping on two internal Routing Tables. Housekeeping in terms of creating these internal Routing Tables according to the Default Gateway of both WAN Interfaces. After doing this I can ping the IP on the Internet with internal Routing Table 0 and afterwards with the internal Routing Table 1.

With the results of this I then can run my logic to select the desired WAN and modify the main routing Table.

Is this how this should be done or is there a way easier approach? Both my WAN Interfaces have dynamic IP addresses. I also want to ping the same target from both WAN Interfaces.

Your problem is you have no clue of the requirement and stuck in another routers method.

Define in terms of functionality without discussing config.
It simply sounds like you want the router to check if the WANS are up or not for example.

Yes, I want a Dual WAN Setup. Both WAN have dynamic IP. It should ping e.g. 8.8.8.8 to check if the WAN interfaces are healthy.

Your topic title is “Dual WAN Failover”

You must use search before open new topic, with same words and…

http://forum.mikrotik.com/t/wan-load-balancing-between-2-isps-one-with-cgnat-and-another-in-bridge-mode-real-ipv4-address/150195/2

I already searched for hours for a ready solution, but didn’t find any ready made solution. I get the IP + Gateway dynamically via DHCP for both WAN. Thus this solution won’t work. Also I don’t want to Ping the Gateway, I want to ping an Endpoint in the Internet. Pinging the Gateway won’t guarantee Internet access!

Do not try to explain that to me, instead read…

You have not even read what I linked, it does not ping the Gateway, but only the remote Endpoint …

check ping 1.1.1.1 instead of ISP1 IP<<<

That methot perfectly work, simply put (already writed on my other posts) a function on DHCP Client, when get an gateway, put it on route…

Easier than that…

Ok, I got confused by isp1gateway and isp2gateway. But I think it still doesn’t really help for my use case. I get the IP + Gateway via DHCP. IP + Gateway is dynamic. So I shouldn’t hard code these into some scripts or routes. Your example hard codes the Gateway into the script, but these could change any time.

search tag # rextended dual dhcp wan failover

This works only on 6.46.8+ and not in v7+

REMEMBER TO REMOVE “ADD DEFAULT ROUTE” ON DHCP CLIENTs
and add those scripts on “script” section of DHCP client

script for DHCP Client 1 = MAIN

/ip route
set [find where comment~"ISP1"] gateway=$"gateway-address"

script for DHCP Client 2 = FAILOVER

/ip route
set [find where comment~"ISP2"] gateway=$"gateway-address"

This must be pasted on new terminal, better if before do that remove all non dynamic routes from routes
DO NOT CHANGE COMMENTS!!! (until keeped ISP1 and ISP2 inside…)
First time change 2.3.4.5 and 6.7.8.9 with actuals or, after paste on new terminal, disable and re-enable dhcp clients

/ip dns
set servers=1.1.1.1,8.8.8.8

/ip route
add comment="A - 1.1.1.1 must be reachable only from ISP1" distance=1 dst-address=1.1.1.1/32 gateway=2.3.4.5 scope=10
add comment="B - Recursive Routing, check ping 1.1.1.1 instead of ISP IP" distance=10 gateway=1.1.1.1 check-gateway=ping
add comment="C - ISP2 is alternative Gateway" distance=20 gateway=6.7.8.9

As rextended stated, look up recursive routing in search!!
http://forum.mikrotik.com/t/advanced-routing-failover-without-scripting/136599/1
is a long winded thread on the topic.

Basically one wants to use existing DNS servers to verify not only if the connection to the ISP server is good but that the connection from the ISP server to the internet is also working.

What you haven’t described as requested is a clear set of requirements.

  1. Is their a primary and secondary gateway
  2. Do you have one subnet that should use one gateway and use the other gateway as backup or not
  3. Do you have another subnet that should use the other gateway and use the gateway in 2. as backup or not.

What type of ISP connections (same provider? different provider? static or dynamic?)


Here is an example of my setup with a primary network of Bell Fibre (1gig up/down) and a secondary Eastlink Cable.
Note that all our email is done through Eastlink.

/ip route
add check-gateway=ping distance=3 gateway=1.0.0.1
add check-gateway=ping distance=4 gateway=9.9.9.9
add comment=PrimaryRecursive distance=3 dst-address=1.0.0.1/32 gateway=
primarygateway_IP scope=10
add comment=SecondaryRecursive distance=4 dst-address=9.9.9.9/32 gateway=
primarygateway_IP scope=10
add comment=SecondaryWAN distance=10 gateway=secondarygateway_IP
add comment=Email_bypass distance=2 dst-address=secondaryISP-emailserver_IP gateway=
secondarygatewayIP

I provide the simplest solution A-B-C and the DHCP Client scripts for “Dual DHCP WAN Failover”,
no matter if one or two are static or dynamic.

Thanks, this looks pretty amazing! I didn’t notice that I can attach scripts to a DHCP Client, this opens whole new possibilities. I’m still a Mikrotik NOOB, sorry for my dumb assumptions.

Disabled default route on the DHCP clients + Added the three routes + Configured the two DHCP clients with the scripts. After toggling the DHCP clients, the routes updated with the correct gateways. That’s already very nice.

I’m now stuck that it looks like the check-gateway fails. Thus it always prefers ISP2.

My routes:

[admin@MikroTik] > /ip/route print
Flags: D - DYNAMIC; I - INACTIVE, A - ACTIVE; c - CONNECT, s - STATIC, y - COPY; H - HW-OFFLOADED
Columns: DST-ADDRESS, GATEWAY, DISTANCE
  #        DST-ADDRESS      GATEWAY       DI
;;; C - ISP2 is alternative Gateway
  0   As   0.0.0.0/0        172.16.50.1   20
;;; B - Recursive Routing, check ping 1.1.1.1 instead of ISP IP
  1   IsH  0.0.0.0/0        1.1.1.1       10
;;; A - 1.1.1.1 must be reachabble only from ISP1
  2   As   1.1.1.1/32       77.109.171.1   1
     DAc   77.109.171.0/24  init7          0
     DAc   172.16.50.0/24   50             0
     DAc   172.16.90.0/24   90             0
     DAc   192.168.88.0/24  lan            0
  3   As   192.168.99.0/24  172.16.50.1    1

Next I checked if the static route for 1.1.1.1/32 worked. This seems to be the case compared to a traceroute with another IP (172.16.50.3 is an external NAT Router behind the same ISP for testing purposes):

[admin@MikroTik] > /tool/traceroute 1.1.1.1
Columns: ADDRESS, LOSS, SENT, LAST, AVG, BEST, WORST, STD-DEV
  #  ADDRESS        LO  S  LAST   AVG  BES  WOR  STD
  1  77.109.183.62  0%  2  7.3ms  7.4  7.3  7.4  0.1
  2  77.109.183.61  0%  2  8ms    7.7  7.4  8    0.3
  3  82.197.168.35  0%  2  9ms    8.4  7.7  9    0.7
  4  194.42.48.14   0%  2  7.7ms  7.6  7.5  7.7  0.1
  5  1.1.1.1        0%  2  7ms    7    6.9  7    0.1

[admin@MikroTik] > /tool/traceroute 8.8.8.8
Columns: ADDRESS, LOSS, SENT, LAST, AVG, BEST, WORST, STD-DEV
  #  ADDRESS         LO  S  LAST   AVG  BES  WOR  S
  1  172.16.50.3     0%  1  0.6ms  0.6  0.6  0.6  0
  2  77.109.183.62   0%  1  7.7ms  7.7  7.7  7.7  0
  3  77.109.183.61   0%  1  7.9ms  7.9  7.9  7.9  0
  4  5.180.135.161   0%  1  7.9ms  7.9  7.9  7.9  0
  5  77.109.135.214  0%  1  7.3ms  7.3  7.3  7.3  0
  6  74.125.243.161  0%  1  8.8ms  8.8  8.8  8.8  0
  7  172.253.50.19   0%  1  8.5ms  8.5  8.5  8.5  0
  8  8.8.8.8         0%  1  7.7ms  7.7  7.7  7.7  0

Did I miss something?

Sorry the other fella will have to help you there, I only use scripts in DHCP client when necessary. Which is mainly to fetch a new gateway IP to stick in routes rules, when my ISP changes my IP address and or power outage or reboot etc…

Its much easier for me to do routing in the routing section and that way one can learn about routing more properly, than instituting a clever shortcut in the client settings.
Thats an advanced bit of work that one can do later once understanding the config of the router more holistically. Thus I dont condone for you doing this in dhcp client scripts.
Thats coming from a guy that is newish in the game without extensive IT experience or training. The other fella is lightyears ahead and thus he dreams this stuff and can quote MT scripts even while intoxicated. PS not insinuating he imbibes or anything just that he has neurons and synapses crammed with MT knowledge.

More importantly you didnt provide the answers to the questions posed 1.,2.3…

I like your method more,
it’s the one I usually use,
but explaining and applying it on the forum is more complicated than a copy and paste… :)))

Yes… you run beta 7, the script and route are for 6.46+ version,
on beta7 the routing is totally different…

You wrote in beginner basics section ,the question for 7 beta must be go on adequate section…

I thought it was self-explanatory LOL.
The point was read the link and then be relieved that the example provided is so simple in comparison to the Russian complex methods LOL.

Note1: Checkgateway ping has the effect of telling the router to keep checking the connection every 10 seconds or so.
If the route is down after two attempts then switch to the other route(s) available on the main table.
Keep checking the route and if it comes back up then go back to the original route.

The recursive bit basically states i want to route to the DNS1 using the gateway through MY ISP for the ping check.
If that is not working then try the alternate DNS2 using the gateway through MY ISP
If that is not working then using my secondary ISP gateway to check connectivity for routing.

They are differentiated by distance! So ISP1 DNS1 is primary, ISP1 DNS2 is secondary, and ISP2 is tertiary. ONe could get fancy and check the backup ISP through DNSs as well but I dont require that level of complication. One doesnt need to ping the backup ISP because as soon as connectivity is established via DNS1 or DNS2 on ISP1, the router will kick the routing back to ISP1.

The email bypass entry simply ensures that any traffic smtp which the router resolves to the IP address of the ISP2 mail server, bypasses ISP1.
I probably dont need the distance setting here but I put it just to amplify the priority of this traffic to myself and or the reader of the config.

/ip route
add check-gateway=ping distance=3 gateway=1.0.0.1
add check-gateway=ping distance=4 gateway=9.9.9.9
add comment=PrimaryRecursive distance=3 dst-address=1.0.0.1/32 gateway=
primarygateway_IP scope=10
add comment=SecondaryRecursive distance=4 dst-address=9.9.9.9/32 gateway=
primarygateway_IP scope=10
add comment=SecondaryWAN distance=10 gateway=secondarygateway_IP
add comment=Email_bypass distance=2 dst-address=secondaryISP-emailserver_IP gateway=
secondarygatewayIP

Hi rextended, my ccr1009 is on version 6 LOL. The RB450Gx4 behind my hex (acting as a switch) and connected to the CCR1009 is running v7 beta6 as a wireguard router,/server mainly for the purposes of connecting to an RB4011 acting as wireguard client router, behind a Bell ISP device (combo modem/router) at a different location.

duplicate post

I have now downgraded my RouterOS to 6 and the Solution with the DHCP Scripts from rextended works perfect. Really happy with it :slight_smile:

Thank you!!!

When the stable v7 comes out in 2023, I will update all my tagged scripts.

Um, sometimes I take too many things for granted…
How can I explain it better?