Skip to content

Instantly share code, notes, and snippets.

@wilsenhc
Last active January 21, 2026 16:05
Show Gist options
  • Select an option

  • Save wilsenhc/f635400c254e68a49159f5e3910f0864 to your computer and use it in GitHub Desktop.

Select an option

Save wilsenhc/f635400c254e68a49159f5e3910f0864 to your computer and use it in GitHub Desktop.
Mikrotik Load Balancer
:local wan1_gateway [/ip route get [find dst-address=0.0.0.0/0 routing-table=wan1] gateway];
:local wan2_gateway [/ip route get [find dst-address=0.0.0.0/0 routing-table=wan2] gateway];
:local wan1_reachable [/ping $wan1_gateway count=3 interval=1];
:if ($wan1_reachable = 0) do={
/ip route set [find dst-address=0.0.0.0/0 routing-table=main] gateway=$wan2_gateway;
:log info "Failover: Switching to wan 2";
} else={
/ip route set [find dst-address=0.0.0.0/0 routing-table=main] gateway=$wan1_gateway;
:log info "Failover: Switching back to wan 1";
}

PROMPT: I have a Mikrotik router running RouterOS 7.17.

I have two internet providers, both use DCHP for providing an IP address.

First provider speed is 300Mbps, second provider speed is 200Mbps.

The first provider, connected to the ether1 interface, and the second provider connected to the ether2 interface of my router.

My local network is using 192.168.88.1/24, defconf from the router.

I also have 2 wifi networks, wifi1 and wifi2, and I need both of them to be able to use the failover connection.

Using RouterOS 7, and using Routing Tables I want to setup my mikrotik router to use the internet connection from the ether1 interface as the main one and fallback to ether2 when ether 1 is down.

RESPONSE:

/ip dhcp-client
add interface=ether1 add-default-route=yes disabled=no

/ip dhcp-client
add interface=ether2 add-default-route=no disabled=no

/routing table
add name=wan1
add name=wan2

/ip route
add dst-address=0.0.0.0/0 gateway=ether1 routing-table=wan1 check-gateway=ping
add dst-address=0.0.0.0/0 gateway=ether2 routing-table=wan2 check-gateway=ping

/ip firewall mangle
add action=mark-routing chain=prerouting in-interface=ether1 new-routing-mark=wan1 passthrough=no
add action=mark-routing chain=prerouting in-interface=ether2 new-routing-mark=wan2 passthrough=no

/ip route
add dst-address=0.0.0.0/0 gateway=wan1 check-gateway=ping

/system script
add name=failover source={
:local wan1_gateway [/ip route get [find dst-address=0.0.0.0/0 routing-table=main] gateway];
:local wan2_gateway [/ip route get [find dst-address=0.0.0.0/0 routing-table=wan2] gateway];
:local wan1_reachable [/ping $wan1_gateway count=3 interval=1];
:if ($wan1_reachable = 0) do={
    /ip route set [find dst-address=0.0.0.0/0 routing-table=main] gateway=$wan2_gateway;
    :log info "Failover: Switching to wan 2";
} else={
    /ip route set [find dst-address=0.0.0.0/0 routing-table=main] gateway=$wan1_gateway;
    :log info "Failover: Switching back to wan 1";
}
} policy=read,write,policy,test,password,sniff,sensitive

:local wan1_gateway [ip route get [find comment~\"Primary WAN - ether1\"] gateway]
:local wan2_gateway [ip route get [find comment~\"Secondary WAN - ether2\"] gateway]
:local wan1_reachable [:ping \$wan1_gateway count=3 interval=1]

:if (\$wan1_reachable = 0) do={
  /ip route disable [find comment~\"Primary WAN - ether1\"]
  /ip route enable [find comment~\"Secondary WAN - ether2\"]
  :log info \"WAN1 down, switched to WAN2\"
} else={
  /ip route enable [find comment~\"Primary WAN - ether1\"]
  /ip route disable [find comment~\"Secondary WAN - ether2\"]
  :log info \"WAN1 up, switched back to WAN1\"
}

/system scheduler
add interval=5s name=failover-check on-event=failover start-time=startup

/ip firewall nat
add action=masquerade chain=srcnat out-interface=ether1
add action=masquerade chain=srcnat out-interface=ether2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment