Community discussions

MikroTik App
 
4920441
just joined
Topic Author
Posts: 21
Joined: Wed Aug 28, 2013 6:03 pm

Loadbalancing multiple DHCP assigned Wan Interfaces to LAN

Mon Nov 17, 2014 6:19 pm

Hi,

I am currently trying to set up the following:

wan1 -> DHCP dynamic (changing!) assigned private Adress from Provider
wan2 -> DHCP dynamic (changing!) assigned private Adress from Provider
wan3 -> DHCP dynamic (changing!) assigned private Adress from Provider
lan -> Routerboard is DHCP Server for local (std.) Adressrange 192.168.88.1 routerip -> 20-200 dhcpserver assigned range.

Every Load Balancing solution I saw so far for RouterOS was for fixed IPs on the wan interfaces.

How do I set up something like http://wiki.mikrotik.com/wiki/NTH_load_ ... masquerade with totally dynamic assigned wan interfaces? I don't know any of the gateways, nor adresses, all i know is that there are all from a non-public IP range....
/ ip address
add address=192.168.0.1/24 network=192.168.0.0 broadcast=192.168.0.255 interface=Local 
add address=10.111.0.2/24 network=10.111.0.0 broadcast=10.111.0.255 interface=wlan2
add address=10.112.0.2/24 network=10.112.0.0 broadcast=10.112.0.255 interface=wlan1

/ ip firewall mangle
add chain=prerouting src-address-list=odd in-interface=Local action=mark-connection \
  new-connection-mark=odd passthrough=yes 
add chain=prerouting src-address-list=odd in-interface=Local action=mark-routing \
  new-routing-mark=odd passthrough=no
add chain=prerouting src-address-list=even in-interface=Local action=mark-connection \
  new-connection-mark=even passthrough=yes 
add chain=prerouting src-address-list=even in-interface=Local action=mark-routing \
  new-routing-mark=even passthrough=no
add chain=prerouting in-interface=Local connection-state=new nth=2,1 \ 
    action=mark-connection new-connection-mark=odd passthrough=yes
add chain=prerouting in-interface=Local action=add-src-to-address-list \
  address-list=odd address-list-timeout=1d connection-mark=odd passthrough=yes 
add chain=prerouting in-interface=Local connection-mark=odd action=mark-routing \ 
    new-routing-mark=odd passthrough=no
add chain=prerouting in-interface=Local connection-state=new nth=2,2 \ 
    action=mark-connection new-connection-mark=even passthrough=yes
add chain=prerouting in-interface=Local action=add-src-to-address-list \
  address-list=even address-list-timeout=1d connection-mark=even passthrough=yes 
add chain=prerouting in-interface=Local connection-mark=even action=mark-routing \ 
    new-routing-mark=even passthrough=no

/ ip firewall nat 
add chain=srcnat out-interface=wlan1 action=masquerade
add chain=srcnat out-interface=wlan2 action=masquerade

/ ip route 
add dst-address=0.0.0.0/0 gateway=10.111.0.1 scope=255 target-scope=10 routing-mark=odd
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 routing-mark=even 
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 

Maybe i don't see the forest for the trees....:-)

Thanks a lot for your help!

Cheers,

4920441
 
4920441
just joined
Topic Author
Posts: 21
Joined: Wed Aug 28, 2013 6:03 pm

Re: Loadbalancing multiple DHCP assigned Wan Interfaces to L

Thu Nov 20, 2014 10:01 am

Reply'ing to myself.... preposterous...:-)

/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1 scope=255 target-scope=10 routing-mark=odd
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 routing-mark=even
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 [/code]

As far as I see it, I "only" need to get the gateway adresses from a script which asks the three interfaces for its assigned gateways - or another script, wich runs in advance store the actual gateway IPs into three variables $gatew1 to $gatew3, which are used as variable in the route command.

Can this be done?

Thanks alot

Cheers,

Prefect
 
bingo220
Member Candidate
Member Candidate
Posts: 124
Joined: Sun Sep 22, 2013 9:30 pm
Location: Ukraine

Re: Loadbalancing multiple DHCP assigned Wan Interfaces to L

Thu Nov 20, 2014 3:41 pm

I use 3 hosts for each ISP to check the availability of the Internet.
This script changes the gateways in the routes if they are changed in the pppoe.
##### Script Settings #####
:local Wan1Name "pppoe1"
:local Wan2Name "pppoe2"
:local ComW1H1 "Static PING way by ISP1-host1"
:local ComW1H2 "Static PING way by ISP1-host2"
:local ComW1H3 "Static PING way by ISP1-host3"
:local ComW2H1 "Static PING way by ISP2-host1"
:local ComW2H2 "Static PING way by ISP2-host2"
:local ComW2H3 "Static PING way by ISP2-host3"
#####################
##### for WAN1 #####
:local Wan1Status
/interface pppoe-client monitor $Wan1Name once do={ :set Wan1Status $status}
:if ($Wan1Status = "connected") do={
  :local GW1new
  /interface pppoe-client monitor $Wan1Name once do={ :set GW1new $"remote-address"};
  :local GW1H1old [/ip route get [find comment="$ComW1H1"] gateway];
  :local GW1H2old [/ip route get [find comment="$ComW1H2"] gateway];
  :local GW1H3old [/ip route get [find comment="$ComW1H3"] gateway];
    :if (($GW1new != $GW1H1old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H1"] gateway=$GW1new;
      :log warning message="GW (Wan1Host1) was changed by script (check-gw) from <$GW1H1old> to <$GW1new>" 
    }
    :if (($GW1new != $GW1H2old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H2"] gateway=$GW1new;
      :log warning message="GW (Wan1Host2) was changed by script (check-gw) from <$GW1H2old> to <$GW1new>" 
    }
    :if (($GW1new != $GW1H3old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H3"] gateway=$GW1new;
      :log warning message="GW (Wan1Host3) was changed by script (check-gw) from <$GW1H3old> to <$GW1new>" 
    }
}

##### for WAN2 #####
:local Wan2Status
/interface pppoe-client monitor $Wan2Name once do={ :set Wan2Status $status}
:if ($Wan2Status = "connected") do={
  :local GW2new
  /interface pppoe-client monitor $Wan2Name once do={ :set GW2new $"remote-address"};
  :local GW2H1old [/ip route get [find comment="$ComW2H1"] gateway];
  :local GW2H2old [/ip route get [find comment="$ComW2H2"] gateway];
  :local GW2H3old [/ip route get [find comment="$ComW2H3"] gateway];
    :if (($GW2new != $GW2H1old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H1"] gateway=$GW2new;
      :log warning message="GW (Wan2Host1) was changed by script (check-gw) from <$GW2H1old> to <$GW2new>" 
    }
    :if (($GW2new != $GW2H2old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H2"] gateway=$GW2new;
      :log warning message="GW (Wan2Host2) was changed by script (check-gw) from <$GW2H2old> to <$GW2new>" 
    }
    :if (($GW2new != $GW2H3old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H3"] gateway=$GW2new;
      :log warning message="GW (Wan2Host3) was changed by script (check-gw) from <$GW2H3old> to <$GW2new>" 
    }
}
 
ryandenis
newbie
Posts: 31
Joined: Fri Aug 29, 2014 7:40 am

Re: Loadbalancing multiple DHCP assigned Wan Interfaces to L

Thu Nov 20, 2014 5:48 pm

I use 3 hosts for each ISP to check the availability of the Internet.
This script changes the gateways in the routes if they are changed in the pppoe.
##### Script Settings #####
:local Wan1Name "pppoe1"
:local Wan2Name "pppoe2"
:local ComW1H1 "Static PING way by ISP1-host1"
:local ComW1H2 "Static PING way by ISP1-host2"
:local ComW1H3 "Static PING way by ISP1-host3"
:local ComW2H1 "Static PING way by ISP2-host1"
:local ComW2H2 "Static PING way by ISP2-host2"
:local ComW2H3 "Static PING way by ISP2-host3"
#####################
##### for WAN1 #####
:local Wan1Status
/interface pppoe-client monitor $Wan1Name once do={ :set Wan1Status $status}
:if ($Wan1Status = "connected") do={
  :local GW1new
  /interface pppoe-client monitor $Wan1Name once do={ :set GW1new $"remote-address"};
  :local GW1H1old [/ip route get [find comment="$ComW1H1"] gateway];
  :local GW1H2old [/ip route get [find comment="$ComW1H2"] gateway];
  :local GW1H3old [/ip route get [find comment="$ComW1H3"] gateway];
    :if (($GW1new != $GW1H1old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H1"] gateway=$GW1new;
      :log warning message="GW (Wan1Host1) was changed by script (check-gw) from <$GW1H1old> to <$GW1new>" 
    }
    :if (($GW1new != $GW1H2old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H2"] gateway=$GW1new;
      :log warning message="GW (Wan1Host2) was changed by script (check-gw) from <$GW1H2old> to <$GW1new>" 
    }
    :if (($GW1new != $GW1H3old) && ($GW1new != "") && ($GW1new != 0)) do={ 
      /ip route set [find comment="$ComW1H3"] gateway=$GW1new;
      :log warning message="GW (Wan1Host3) was changed by script (check-gw) from <$GW1H3old> to <$GW1new>" 
    }
}

##### for WAN2 #####
:local Wan2Status
/interface pppoe-client monitor $Wan2Name once do={ :set Wan2Status $status}
:if ($Wan2Status = "connected") do={
  :local GW2new
  /interface pppoe-client monitor $Wan2Name once do={ :set GW2new $"remote-address"};
  :local GW2H1old [/ip route get [find comment="$ComW2H1"] gateway];
  :local GW2H2old [/ip route get [find comment="$ComW2H2"] gateway];
  :local GW2H3old [/ip route get [find comment="$ComW2H3"] gateway];
    :if (($GW2new != $GW2H1old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H1"] gateway=$GW2new;
      :log warning message="GW (Wan2Host1) was changed by script (check-gw) from <$GW2H1old> to <$GW2new>" 
    }
    :if (($GW2new != $GW2H2old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H2"] gateway=$GW2new;
      :log warning message="GW (Wan2Host2) was changed by script (check-gw) from <$GW2H2old> to <$GW2new>" 
    }
    :if (($GW2new != $GW2H3old) && ($GW2new != "") && ($GW2new != 0)) do={ 
      /ip route set [find comment="$ComW2H3"] gateway=$GW2new;
      :log warning message="GW (Wan2Host3) was changed by script (check-gw) from <$GW2H3old> to <$GW2new>" 
    }
}
I have the same setup (3 ISP's some dynamic, some static) and would love to see an example of the full config to make this work. I've tried multiple examples of the various types of load balancing available and none work.
 
error216216
newbie
Posts: 25
Joined: Fri May 23, 2014 2:07 am

Re: Loadbalancing multiple DHCP assigned Wan Interfaces to L

Fri Nov 21, 2014 7:13 am

Ok, I personnaly use PCC load balanceing because I have a connection that is slower and one that is faster and I want the users to use bouth ISP's not be assigned for one day only to one but if NTH load balanceing is the way you want to go, here's the mangle script for 3 connections:
/ ip firewall mangle
add chain=prerouting src-address-list=odd in-interface=Local action=mark-connection \
  new-connection-mark=odd passthrough=yes 
add chain=prerouting src-address-list=odd in-interface=Local action=mark-routing \
  new-routing-mark=odd passthrough=no
add chain=prerouting src-address-list=even in-interface=Local action=mark-connection \
  new-connection-mark=even passthrough=yes 
add chain=prerouting src-address-list=even in-interface=Local action=mark-routing \
  new-routing-mark=even passthrough=no
### Adding a rule to handle the third connection traffic ###
add chain=prerouting src-address-list=third in-interface=Local action=mark-connection \
  new-connection-mark=third passthrough=yes 
add chain=prerouting src-address-list=third in-interface=Local action=mark-routing \
  new-routing-mark=third passthrough=no
### End of rule to handle the third connection traffic ###
### The next rules will assign a new unmarked connection from a client to a address list that decides what connection that client will use for the next day ###
add chain=prerouting in-interface=Local connection-state=new nth=3,1 \ 
    action=mark-connection new-connection-mark=odd passthrough=yes
add chain=prerouting in-interface=Local action=add-src-to-address-list \
  address-list=odd address-list-timeout=1d connection-mark=odd passthrough=yes 
add chain=prerouting in-interface=Local connection-mark=odd action=mark-routing \ 
    new-routing-mark=odd passthrough=no
add chain=prerouting in-interface=Local connection-state=new nth=3,2 \ 
    action=mark-connection new-connection-mark=even passthrough=yes
add chain=prerouting in-interface=Local action=add-src-to-address-list \
  address-list=even address-list-timeout=1d connection-mark=even passthrough=yes 
add chain=prerouting in-interface=Local connection-mark=even action=mark-routing \ 
    new-routing-mark=even passthrough=no
add chain=prerouting in-interface=Local connection-state=new nth=3,3 \ 
    action=mark-connection new-connection-mark=third passthrough=yes
add chain=prerouting in-interface=Local action=add-src-to-address-list \
  address-list=third address-list-timeout=1d connection-mark=third passthrough=yes 
add chain=prerouting in-interface=Local connection-mark=third action=mark-routing \ 
    new-routing-mark=third passthrough=no
Now the NAT Rules:
add chain=srcnat out-interface=wan1 action=masquerade
add chain=srcnat out-interface=wan2 action=masquerade
add chain=srcnat out-interface=wan3 action=masquerade
Now if you have static gateways the routes would look something like this:
Assuming ISP1GW=10.0.0.1 ISP2GW=10.0.1.1 ISP3GW=10.0.2.1
/ ip route 
add dst-address=0.0.0.0/0 gateway=10.0.0.1 scope=255 target-scope=10 routing-mark=odd 
add dst-address=0.0.0.0/0 gateway=10.0.1.1 scope=255 target-scope=10 routing-mark=even
add dst-address=0.0.0.0/0 gateway=10.0.2.1 scope=255 target-scope=10 routing-mark=third
### You can add one or more routes separated by distance for the router's traffic, like NTP and other stuff: ###
add distance=1 dst-address=0.0.0.0/0 gateway=10.0.0.1 check-gateway=ping
add distance=2 dst-address=0.0.0.0/0 gateway=10.0.1.1 check-gateway=ping
add distance=3 dst-address=0.0.0.0/0 gateway=10.0.2.1 check-gateway=ping
### Or if you use dhcp client just check add default route on one or more of them and the 3 rules above are not necessary ###
Ok so now let's say you have dynamic ip addresses, the simplest method that does not apply to everyone is to see if the ISP is using the same subnet, if that is the case you can determine what the gateway that you will recive every time and use the above config.
For example let's say that ISP1 is using subnet 10.0.0.0/16, and you recive IP's from 10.0.0.2 to 10.0.255.254 but the gateway is always 10.0.0.1, that means that you can use 10.0.0.1 in the route for the ISP1 as if it was static:
/ ip route 
add dst-address=0.0.0.0/0 gateway=10.0.0.1 scope=255 target-scope=10 routing-mark=odd
### This route will be valid in this case eaven if ip changes in the subnet###
Now the tricky part, let's assume the ISP is using multiple subnets so one time you recive 192.168.0.55 with gateway 192.168.0.1, another time you recive 192.168.66.22 with gateway 192.168.66.254, there is no way to predict this so you will have to use a script that checks if the gateway changed and if it did to modify the route for that ISP.
Example:
/system script
add name=CheckISP1GW policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":global gate\
    way1;\r\
    \n### Assuming that the DHCP Client rule for ISP1 is on position 0 we do: ###\r\
    \nif ( \$gateway1 != [/ip dhcp-client get 0 gateway] ) do {\r\
    \n:set \$gateway1 [/ip dhcp-client get 0 gateway];\r\
    \n### Assuming that the routing rule for ISP1 is on position 0 we do: ###\r\
    \n/ip route set 0 gateway=\$gateway;\r\
    \n}"
### Now we set up a schedule to run this script every 30 seconds ###
add disabled=no interval=30s name=CheckISP1GW on-event="/system script run CheckISP1GW" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=nov/21/2014 start-time=0

Who is online

Users browsing this forum: Amazon [Bot] and 31 guests