Skip to content

Pratama Digital

Your Digital Partner

Menu
  • Home
  • Download Area
  • Pratama Digital Net
  • Internet Speed Test
  • About Us
Menu

Script Load balancing mikrotik 3 ISP with ECMP

Posted on September 16, 2023

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.

Share this:

  • Facebook
  • X

Related

Leave a Reply Cancel reply

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

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages

Tags

24h clock adobe adobe cc adobe cs master almalinux arduino autodesk basic ip address cad software calculator php connect java dns server download photoshop ecmp formula ipv4 innodb install iptables ipsec iptables java mysql linux firewall linux mint linux security load balance microsoft office myisam mysql mysql engine node.js nodejs nth office alternative openvpn pcc php port game online port mikrotik postgresql postgresql to mysql public dns rocky linux running text script php ubuntu LTS wireguard

Categories

  • Advertising
  • CAD software
  • Computer Component
  • Control Panel
  • Database
  • DNS
  • Download
  • File Server
  • Firewall
  • Graphic Design
  • Internet
  • IP Address
  • Linux OS
  • Load Balance
  • Mikrotik
  • Movie
  • Novel/Book
  • Office
  • OpenWRT
  • Processor & GPU
  • Programming
  • Routing
  • Security
  • Server
  • Tutorial
  • Video Editing
  • VPN
  • Web Programming
  • Windows OS
  • Wordpress

About Us

We are a company specializing in network configuration, MikroTik and computer installation services, as well as wifi setup. With experience dating back to 2017, we have gained the trust of numerous clients from various regions who rely on our services. Our expertise extends to catering to personal, office, institutional, and industrial needs.

Archives

  • December 2024
  • September 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages
©2025 Pratama Digital | Design: Newspaperly WordPress Theme