Mikrotik

I need to setup something that seems simple to me in my mind but I don't work intimately enough with RouterOS to be able to do it quickly and easily.

Would someone be prepared to quote on writing the necessary scripts based on the requirement below?

High Level Requirement:
- Two ISP's; ISP1 active 17h55 - 23h45; ISP2 - active 24/7
- Approx 24 devices on the network

I want to
- create two pools of IP addresses
- assign about 18 of the devices to the one pool and the rest to the other pool
- Force devices in Pool 1 to use ISP1 during it's active time period (the rest of the time use ISP2)
- Force devices in Pool 2 to use ISP2 all the time

So I know that I need to do the following:

a. Setup the Pools
b. Manually assign the devices to respective pools (via Static IP's)
c. Create Mangle Rules and Mark the traffic from the pools/devices in order to
d. Use the Marks to route the traffic to the respective ISP's
The reason for this is that ISP1 is uncapped but constrained to 10 meg - the other is capped but runs at 40, and there are 4 specific devices that I want to be running on the 40 meg line all the time (the 10 meg uncapped ISP is fine for the kids YouTube and Netflix).

PS I currently have simple queues and scripts in place that switches to the appropriate ISP on a time based schedule.
You'll have to use Queues, and mark the packets to determine the path they should follow.
 
You'll have to use Queues, and mark the packets to determine the path they should follow.

:D I know :D but like I said, no time to delve into RouterOS - ag, I've looked at all the examples and know more or less what to do but it's going to take so freaking long for me to do it that I'll happily pay (a reasonable amount) for someone to just give me the scripts (happy to tweak and modify them to suit my installation but need them to be written with the above spec in mind)

So script must contain the following aspects:

1. Setup pools
2. Create router mark depending on which pool an IP is in
3. Setup Queues to route traffic based on the router mark
4. Take into account that ISP1 is only active during a specific time period

So, I know what to do, I'm just not proficient enough in RouterOS to do it myself

Edit: I know enough about the syntax to follow precisely what the script is doing (hence happy to tweak)

Edit 2: Also want someone who is experienced to ensure that the script takes care of the things I haven't thought about
 
Last edited:
Thanks to @biena, all sorted out and working like a charm.

Added bonus is that got me fiddling about and I've now also got a real time traffic monitor (by local IP) up and running and hardened my router a bit more.
 
Thanks to @biena, all sorted out and working like a charm.

Added bonus is that got me fiddling about and I've now also got a real time traffic monitor (by local IP) up and running and hardened my router a bit more.
How did you do the real-time monitoring?
 
Anyone here that could maybe help me with my little project im trying to get going. Got 2 Rain routers that i want to run on the same Mikrotik.

Eth1"WAN Rain Uncapped"
Eth2"Rain Capped"
Eth3-5+WLan

EtherNet 1 & 2 must both get IP address's from the respective routers
Then 3-5+WLan is for the house etc to access the network so DCHP Server on those.
Now what i have done is installed Sonoff on the WAN1 so that it will turn off between 6PM and 11PM.
So during that time i would like to have the Mikrotik failover to Eth2 and then at 11:01PM the Eth1 will turn back on and the failover will switch back. Is there anyone here that would maybe be able to help me with this, i would preffer it if you could remote in and explain to me how you going about it. Dont mind giving you R100 or so if you can get it working. Just PM me if you are able to help.

Many thanks in advance
 
Anyone here that could maybe help me with my little project im trying to get going. Got 2 Rain routers that i want to run on the same Mikrotik.

Eth1"WAN Rain Uncapped"
Eth2"Rain Capped"
Eth3-5+WLan

EtherNet 1 & 2 must both get IP address's from the respective routers
Then 3-5+WLan is for the house etc to access the network so DCHP Server on those.
Now what i have done is installed Sonoff on the WAN1 so that it will turn off between 6PM and 11PM.
So during that time i would like to have the Mikrotik failover to Eth2 and then at 11:01PM the Eth1 will turn back on and the failover will switch back. Is there anyone here that would maybe be able to help me with this, i would preffer it if you could remote in and explain to me how you going about it. Dont mind giving you R100 or so if you can get it working. Just PM me if you are able to help.

Many thanks in advance

What IP addresses are being used for eth1 and eth2?

I personally think the sonoff thing is the wrong way to go. Adding another device to the equation is never a good idea.

But you could have multiple gateways like this: http://www.prinmath.com/ham/mikrotik-failover.htm

A better alternative is to use scheduler in the mikrotik to run a command to switches gateways at 11pm and 6am

What IP addresses are you getting from the 2 routers?
 
Agreed,the analogue way of doing things would be how less intelligent routers would need to handle things

As it stands,add a scheduled task that does
/ip route set [/ip route find dst-address=0.0.0.0/0] gateway=xxx.xxx.xxx.xxx
And change the gateways depending on the time of the script
 
What IP addresses are being used for eth1 and eth2?

I personally think the sonoff thing is the wrong way to go. Adding another device to the equation is never a good idea.

But you could have multiple gateways like this: http://www.prinmath.com/ham/mikrotik-failover.htm

A better alternative is to use scheduler in the mikrotik to run a command to switches gateways at 11pm and 6am

What IP addresses are you getting from the 2 routers?
I would love to use the script method but im no good at that. Miro guys also wouldnt assist me regarding such they useless since the one guy there left.

My details are

Router 1 gives 192.168.3.1/24
Gateway 192.168.3.2

Router 2 gives 192.168.8.106/24
Gateway: 192.168.8.1
______________________________________
WAN3-5+Wifi
Local IP: 192.168.10.1/24
 
I would love to use the script method but im no good at that. Miro guys also wouldnt assist me regarding such they useless since the one guy there left.

My details are

Router 1 gives 192.168.3.1/24
Gateway 192.168.3.2

Router 2 gives 192.168.8.106/24
Gateway: 192.168.8.1
______________________________________
WAN3-5+Wifi
Local IP: 192.168.10.1/24
Am assuming all the ip addreses are static? Or are they via DHCP?

Please also screenshot your route list and NAT rules

Is this working at the moment?
 
I would love to use the script method but im no good at that. Miro guys also wouldnt assist me regarding such they useless since the one guy there left.

My details are

Router 1 gives 192.168.3.1/24
Gateway 192.168.3.2

Router 2 gives 192.168.8.106/24
Gateway: 192.168.8.1
______________________________________
WAN3-5+Wifi
Local IP: 192.168.10.1/24

Think you should be able to modify the script below to suite your needs:
https://wiki.mikrotik.com/wiki/Failover_Scripting

Modified the script a bit, but not did not have time to test it. Easiest to upload it with WinBox (used loadbalancingscript.rsc as name)

/system/scripts

Script changes the distance on the routes, does not explicitly disable the one or the other. Added PreferISP2 based on the current time to the distance metric. So after 6pm ISP1 will have whatever it distance would have been +2.

Code:
# ------------------- header -------------------
# Script by Tomas Kirnak, version 1.0.7
# If you use this script, or edit and
# re-use it, please keep the header intact.
#
# For more information and details about
# this script please visit the wiki page at
# http://wiki.mikrotik.com/wiki/Failover_Scripting
# ------------------- header -------------------



# ------------- start editing here -------------
# Edit the variables below to suit your needs

# Please fill the WAN interface names
:local InterfaceISP1 ether1
:local InterfaceISP2 ether3

# Please fill the ping check host - currently: resolver1.opendns.com
:local PingTarget 208.67.222.222

# Please fill how many ping failures are allowed before fail-over happends
:local FailTreshold 3

# Define the distance increase of a route when it fails
:local DistanceIncrease 2

# Editing the script after this point may break it
# -------------- stop editing here --------------



# Declare the global variables
:global PingFailCountISP1
:global PingFailCountISP2


# This inicializes the PingFailCount variables, in case this is the 1st time the script has ran
:if ([:typeof $PingFailCountISP1] = "nothing") do={:set PingFailCountISP1 0}
:if ([:typeof $PingFailCountISP2] = "nothing") do={:set PingFailCountISP2 0}

# Check the time
:local PreferISP2
:if ([:typeof $PreferISP2] = "nothing) do={:set PreferISP2 0}
:if ([:pick [/system clock get time] 0 2] >= 6) do={:set PreferISP2 0} || ([:pick [/system clock get time] 0 2] <= 23) do={:set PreferISP2 0} else {:set PreferISP2 1}

# This variable will be used to keep results of individual ping attempts
:local PingResult



# Check ISP1
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP1]
:put $PingResult

:if ($PingResult = 0) do={
    :if ($PingFailCountISP1 < ($FailTreshold+2)) do={
        :set PingFailCountISP1 ($PingFailCountISP1 + 1)
        
        :if ($PingFailCountISP1 = $FailTreshold) do={
            :log warning "ISP1 has a problem en route to $PingTarget - increasing distance of routes."
            :foreach i in=[/ip route find gateway=$InterfaceISP1 && static] do=\
                {/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease*(1 + $PreferISP2))}
            :log warning "Route distance increase finished."
        }
    }
}
:if ($PingResult = 1) do={
    :if ($PingFailCountISP1 > 0) do={
        :set PingFailCountISP1 ($PingFailCountISP1 - 1)
        
        :if ($PingFailCountISP1 = ($FailTreshold -1)) do={
            :log warning "ISP1 can reach $PingTarget again - bringing back original distance of routes."
            :foreach i in=[/ip route find gateway=$InterfaceISP1 && static] do=\
                {/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease*($PreferISP2 - 1))}
            :log warning "Route distance decrease finished."
        }
    }
}



# Check ISP2
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP2]
:put $PingResult

:if ($PingResult = 0) do={
    :if ($PingFailCountISP2 < ($FailTreshold+2)) do={
        :set PingFailCountISP2 ($PingFailCountISP2 + 1)
        
        :if ($PingFailCountISP2 = $FailTreshold) do={
            :log warning "ISP2 has a problem en route to $PingTarget - increasing distance of routes."
            :foreach i in=[/ip route find gateway=$InterfaceISP2 && static] do=\
                {/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}
            :log warning "Route distance increase finished."
        }
    }
}
:if ($PingResult = 1) do={
    :if ($PingFailCountISP2 > 0) do={
        :set PingFailCountISP2 ($PingFailCountISP2 - 1)
        
        :if ($PingFailCountISP2 = ($FailTreshold -1)) do={
            :log warning "ISP2 can reach $PingTarget again - bringing back original distance of routes."
            :foreach i in=[/ip route find gateway=$InterfaceISP2 && static] do=\
                {/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
            :log warning "Route distance decrease finished."
        }
    }
}

Run the following from a RouterOS terminal:

Code:
# Remove ether3 from bridge, ether1 is removed by default and ether2 acts as the master for the bridge network
/interface bridge port remove numbers=[/interface bridge port find interface=ether3]

# Get DHCP on non-default WAN
/ip dhcp-client add interface=ether3

/ip route add dst-address=0.0.0.0/0 gateway=ether1 distance=1
/ip route add dst-address=0.0.0.0/0 gateway=ether3 distance=1

 /system scheduler add interval=5s on-event=loadbalancingscript.rsc name=loadbalancing
 
Last edited:
I finally got this all to work, was a little bit of a mission but if someone could help me with the script method to set it via time rather i would love that. Thanks
 
Top
Sign up to the MyBroadband newsletter
X