2 Wan Load Balance

2 Wan Dynamic Dhcp-Client Load Balance


Mikrotik Dynamic Dhcp-Client Load balance script.

Kindly do change the following as mention below.

ether1 = ether1-gateway
ether2 = ether2-gateway
ether3 = ether3

Ether3 is a Lan Interface you can rename after complete 7 steps .

Step 1.
Add dynamic client on ether1 & ether2 (WAN1 & WAN2)

/ip dhcp-client
add add-default-route=no dhcp-options=hostname,clientid disabled=no interface=ether1-gateway use-peer-dns=no use-peer-ntp=no
add add-default-route=no dhcp-options=hostname,clientid disabled=no interface=ether2-gateway use-peer-dns=no use-peer-ntp=no

Step 2: Add Lan Interface IP . ( ether3 )

/ip address
add address=192.168.88.1/24 interface=ether3 network=192.168.88.0

Step 3: Add Firewall Nat rule.

/ip firewall nat
add action=masquerade chain=srcnat out-interface=ether1-gateway
add action=masquerade chain=srcnat out-interface=ether2-gateway

Step 4:  Add firewall Mangle Rules

/ip firewall mangle
add action=mark-connection chain=prerouting comment="Load Balance" connection-state=new in-interface=ether1-gateway new-connection-mark=ether1_conn
add action=mark-connection chain=prerouting connection-state=new in-interface=ether2-gateway new-connection-mark=ether2_conn
add action=mark-routing chain=output connection-mark=ether1_conn new-routing-mark=to_ether1
add action=mark-routing chain=output connection-mark=ether2_conn new-routing-mark=to_ether2
add action=mark-connection chain=prerouting connection-state=new dst-address-type=!local in-interface=ether3 new-connection-mark=ether1_conn per-connection-classifier=both-ports:2/0
add action=mark-connection chain=prerouting connection-state=new dst-address-type=!local in-interface=ether3 new-connection-mark=ether2_conn per-connection-classifier=both-ports:2/1
add action=mark-routing chain=prerouting connection-mark=ether1_conn in-interface=ether3 new-routing-mark=to_ether1
add action=mark-routing chain=prerouting connection-mark=ether2_conn in-interface=ether3 new-routing-mark=to_ether2

Step 5: Add Routes ( Setting temporary  gateway)

/ip route
add check-gateway=ping comment="Ether1-Wan routing gateway" distance=1 gateway=192.168.0.1 routing-mark=to_ether1
add check-gateway=ping comment="Ether2-Wan routing gateway" distance=1 gateway=192.168.1.1 routing-mark=to_ether2
add comment=Ether1-Wan distance=1 gateway=192.168.0.1
add comment=Ether2-Wan distance=2 gateway=192.168.1.1

step 6: Create New Script with name ChangeGateways and copy below lines.

:global newgw [/ip dhcp-client get [find interface="ether1-gateway" ] gateway ]
:global activegw [/ip route get [/ip route find comment="Ether1-Wan"] gateway ]
:if ($newgw != $activegw) do={
/ip route set [find comment="Ether1-Wan"] gateway=$newgw
/ip route set [find comment="Ether1-Wan routing gateway"] gateway=$newgw
}
:global newgw [/ip dhcp-client get [find interface="ether2-gateway" ] gateway ]
:global activegw [/ip route get [/ip route find comment="Ether2-Wan"] gateway ]
:if ($newgw != $activegw) do={
/ip route set [find comment="Ether2-Wan"] gateway=$newgw
/ip route set [find comment="Ether2-Wan routing gateway"] gateway=$newgw
}

Step 7: Final Step.

Now add schedule to run this script with 2 seconds timmer ..

Best Regard’s
Abubaker Siddiq Lasania
Virtual IT Support

Join in @ Page / Group

6 comments

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.