Month: March 2016

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= interface=ether3 network=

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= routing-mark=to_ether1
add check-gateway=ping comment="Ether2-Wan routing gateway" distance=1 gateway= routing-mark=to_ether2
add comment=Ether1-Wan distance=1 gateway=
add comment=Ether2-Wan distance=2 gateway=

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