Creating a script for load balancing in MikroTik using Equal Cost Multi-Path (ECMP) with three Internet Service Providers (ISPs) can help ensure that your network remains reliable and responsive. ECMP distributes network traffic evenly across multiple paths to optimize performance and fault tolerance. In this script, we’ll configure MikroTik to balance traffic across three ISPs with ECMP. Please note that this script assumes you have a basic understanding of MikroTik RouterOS and have configured your router appropriately.

# Load Balancing with ECMP Script for MikroTik
# Ensure you have already configured the WAN interfaces and IP addresses.

# Define variables for ISP gateways
:local isp1 “WAN1-Gateway-IP”
:local isp2 “WAN2-Gateway-IP”
:local isp3 “WAN3-Gateway-IP”

# Define variables for WAN interface names
:local wan1 “ether1-WAN1”
:local wan2 “ether2-WAN2”
:local wan3 “ether3-WAN3”

# Define the IP addresses you want to monitor (Google’s DNS servers)
:local target1 “8.8.8.8”
:local target2 “8.8.4.4”

# Define the routing mark for load balancing
:local lbMark “lb-mark”

# Enable IP Firewall connection tracking for ECMP
/ip firewall connection tracking set enabled=yes generic-timeout=10m

# Add a mangle rule to mark connections for load balancing
/ip firewall mangle add action=mark-connection chain=prerouting connection-state=new in-interface=$wan1 new-connection-mark=$lbMark passthrough=yes
/ip firewall mangle add action=mark-connection chain=prerouting connection-state=new in-interface=$wan2 new-connection-mark=$lbMark passthrough=yes
/ip firewall mangle add action=mark-connection chain=prerouting connection-state=new in-interface=$wan3 new-connection-mark=$lbMark passthrough=yes

# Add a NAT rule for masquerading the marked connections
/ip firewall nat add action=masquerade chain=srcnat connection-mark=$lbMark out-interface-list=WAN

# Add static routes for each ISP
/ip route add distance=1 gateway=$isp1
/ip route add distance=1 gateway=$isp2
/ip route add distance=1 gateway=$isp3

# Add a default route using ECMP
/ip route add check-gateway=ping distance=1 dst-address=0.0.0.0/0 gateway=$isp1 routing-mark=$lbMark scope=30 target-scope=10
/ip route add check-gateway=ping distance=1 dst-address=0.0.0.0/0 gateway=$isp2 routing-mark=$lbMark scope=30 target-scope=10
/ip route add check-gateway=ping distance=1 dst-address=0.0.0.0/0 gateway=$isp3 routing-mark=$lbMark scope=30 target-scope=10

# Monitor the target IP addresses for each ISP and change routing if necessary
/system scheduler add interval=1m name=schedulerECMP on-event=schedulerECMP policy=read,write start-time=startup
/system script add name=schedulerECMP owner=admin policy=read,write source={
:local activeISP [/ip route get [/ip route find routing-mark=$lbMark] gateway]

:local target1Reachable [:len [/ping $target1 interval=1 count=3]]
:local target2Reachable [:len [/ping $target2 interval=1 count=3]]

:if ($target1Reachable < 3) do={
:if ($activeISP != $isp1) do={
/ip route set [find where gateway=$isp1 routing-mark=$lbMark] check-gateway=ping
/ip route set [find where gateway=$isp2 routing-mark=$lbMark] check-gateway=ping-disabled
/ip route set [find where gateway=$isp3 routing-mark=$lbMark] check-gateway=ping-disabled
}
} else={
:if ($target2Reachable < 3) do={
:if ($activeISP != $isp2) do={
/ip route set [find where gateway=$isp1 routing-mark=$lbMark] check-gateway=ping-disabled
/ip route set [find where gateway=$isp2 routing-mark=$lbMark] check-gateway=ping
/ip route set [find where gateway=$isp3 routing-mark=$lbMark] check-gateway=ping-disabled
}
} else={
:if ($activeISP != $isp3) do={
/ip route set [find where gateway=$isp1 routing-mark=$lbMark] check-gateway=ping-disabled
/ip route set [find where gateway=$isp2 routing-mark=$lbMark] check-gateway=ping-disabled
/ip route set [find where gateway=$isp3 routing-mark=$lbMark] check-gateway=ping
}
}
}
}

# Enable the scheduler
/system scheduler enable schedulerECMP

 

Please replace the placeholders (e.g., “WAN1-Gateway-IP,” “ether1-WAN1,” “8.8.8.8”) with your actual ISP gateway IPs, WAN interface names, and target IP addresses for monitoring. Also, ensure you have already configured your WAN interfaces and IP addresses.

This script configures ECMP load balancing with monitoring of target IPs. If one of the targets becomes unreachable on a specific ISP, the script will adjust the routing accordingly to maintain network availability. Adjust the interval and count values in the ping commands as needed for your network’s requirements.

Make sure to backup your configuration before applying this script, as improper configuration can lead to network disruptions. Test it in a controlled environment before deploying it in a production network.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *