Questo forum fa uso dei cookie
Questo forum utilizza i cookie per memorizzare i dettagli del tuo login o della tua ultima visita. I cookie sono piccoli files di testo salvati nel computer; i cookie da noi utilizzati sono relativi unicamente ai servizi da noi forniti direttamente o dai banner pubblicitari. I cookie su questo forum salvano inoltre i dettagli relativi alle discussioni lette e alle tue preferenze personali. Sei pregato di selezionare il tasto OKAY se sei consapevole della presenza di questi files e ci autorizzi ad utilizarli per le informazioni specificate.

Indipendentemente dalla tua scelta un cookie verrà salvato per memorizzare nel tuo pc la risposta a questo form. Puoi modificare le impostazioni relative ai cookie nelle preferenze del tuo browser.

  • 0 voto(i) - 0 media
  • 1
  • 2
  • 3
  • 4
  • 5
[Mikrotik] Failover Scripting

#1
Information 
Basic Info
This is an easy to use script for your failover needs. It is currently written for 2 connections and works by pinging any IP address you specify using both of your connections.
If a ping failure is detected a certain number of times, the script increases distance of all default routes that use the gateway present on the interface that failed the ping.
What to configure
To get this script working you need to fill in these values:

Exact names of both interfaces you use to connect to the internet.
IPs of both gateways that you use.
An IP address that you want to check connectivity against.
The ammount of ping failures after which the script fails-over.
The distance that the default routes will get increased by in order to become secondary.
The script
Codice:
# ------------------- header -------------------

Codice:
# Script by Tomas Kirnak, version 1.0.7

Codice:
# If you use this script, or edit and

Codice:
# re-use it, please keep the header intact.

Codice:
#

Codice:
# For more information and details about

Codice:
# this script please visit the wiki page at

Codice:
# http://wiki.mikrotik.com/wiki/Failover_Scripting

Codice:
# ------------------- header -------------------




Codice:
# ------------- start editing here -------------

Codice:
# Edit the variables below to suit your needs


Codice:
# Please fill the WAN interface names

Codice:
:local InterfaceISP1 ether1

Codice:
:local InterfaceISP2 ether2


Codice:
# Please fill the gateway IPs (or interface names in case of PPP)

Codice:
:local GatewayISP1 1.1.1.1

Codice:
:local GatewayISP2 2.2.2.2


Codice:
# Please fill the ping check host - currently: resolver1.opendns.com

Codice:
:local PingTarget 208.67.222.222


Codice:
# Please fill how many ping failures are allowed before fail-over happends

Codice:
:local FailTreshold 3


Codice:
# Define the distance increase of a route when it fails

Codice:
:local DistanceIncrease 2


Codice:
# Editing the script after this point may break it

Codice:
# -------------- stop editing here --------------




Codice:
# Declare the global variables

Codice:
:global PingFailCountISP1

Codice:
:global PingFailCountISP2


Codice:
# This inicializes the PingFailCount variables, in case this is the 1st time the script has ran

Codice:
:if ([:typeof $PingFailCountISP1] = "nothing") do={:set PingFailCountISP1 0}

Codice:
:if ([:typeof $PingFailCountISP2] = "nothing") do={:set PingFailCountISP2 0}


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

Codice:
:local PingResult




Codice:
# Check ISP1

Codice:
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP1]

Codice:
:put $PingResult


Codice:
:if ($PingResult = 0) do={

Codice:
    :if ($PingFailCountISP1 < ($FailTreshold+2)) do={

Codice:
        :set PingFailCountISP1 ($PingFailCountISP1 + 1)


Codice:
        :if ($PingFailCountISP1 = $FailTreshold) do={

Codice:
            :log warning "ISP1 has a problem en route to $PingTarget - increasing distance of routes."

Codice:
            :foreach i in=[/ip route find gateway=$GatewayISP1 && static] do=\

Codice:
                {/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}

Codice:
            :log warning "Route distance increase finished."

Codice:
        }

Codice:
    }

Codice:
}

Codice:
:if ($PingResult = 1) do={

Codice:
    :if ($PingFailCountISP1 > 0) do={

Codice:
        :set PingFailCountISP1 ($PingFailCountISP1 - 1)


Codice:
        :if ($PingFailCountISP1 = ($FailTreshold -1)) do={

Codice:
            :log warning "ISP1 can reach $PingTarget again - bringing back original distance of routes."

Codice:
            :foreach i in=[/ip route find gateway=$GatewayISP1 && static] do=\

Codice:
                {/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}

Codice:
            :log warning "Route distance decrease finished."

Codice:
        }

Codice:
    }

Codice:
}




Codice:
# Check ISP2

Codice:
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP2]

Codice:
:put $PingResult


Codice:
:if ($PingResult = 0) do={

Codice:
    :if ($PingFailCountISP2 < ($FailTreshold+2)) do={

Codice:
        :set PingFailCountISP2 ($PingFailCountISP2 + 1)


Codice:
        :if ($PingFailCountISP2 = $FailTreshold) do={

Codice:
            :log warning "ISP2 has a problem en route to $PingTarget - increasing distance of routes."

Codice:
            :foreach i in=[/ip route find gateway=$GatewayISP2 && static] do=\

Codice:
                {/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}

Codice:
            :log warning "Route distance increase finished."

Codice:
        }

Codice:
    }

Codice:
}

Codice:
:if ($PingResult = 1) do={

Codice:
    :if ($PingFailCountISP2 > 0) do={

Codice:
        :set PingFailCountISP2 ($PingFailCountISP2 - 1)


Codice:
        :if ($PingFailCountISP2 = ($FailTreshold -1)) do={

Codice:
            :log warning "ISP2 can reach $PingTarget again - bringing back original distance of routes."

Codice:
            :foreach i in=[/ip route find gateway=$GatewayISP2 && static] do=\

Codice:
                {/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}

Codice:
            :log warning "Route distance decrease finished."


To Do
Implement DNS resolving for the PingTarget variable, while keeping it functioning if an IP only is entered.
The script currently auto fall-backs to the primary connection if it becomes responsive again. Implementing a variable that would enable/or disable this might be usefull.
Make the script usable for more then 2 connections while not making it terribly complicated to configure.
Implement a variable that would enable or disable logging output.
Cita messaggio


Discussioni simili
Discussione Autore Risposte Letto Ultimo messaggio
Information [Mikrotik] Failover via Netwatch III (English) berryberry 0 1'024 16-06-2015, 10:01
Ultimo messaggio: berryberry
Information [Mikrotik] Resilience /Monitoring -ECMP Failover Script berryberry 0 1'149 16-06-2015, 09:45
Ultimo messaggio: berryberry
Information [Mikrotik] Using Fetch and Scripting to add IP Address Lists berryberry 0 998 16-06-2015, 08:45
Ultimo messaggio: berryberry
Information [Mikrotik] Using scripting to overcome the inability to specify number ranges on the berryberry 0 692 11-06-2015, 10:52
Ultimo messaggio: berryberry

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Utenti che stanno guardando questa discussione:
1 Ospite(i)


Powered by MyBB, © 2002-2020 MyBB Group.