0% found this document useful (0 votes)
567 views3 pages

Script V7-Redes Brasil SUPOSTO

Uploaded by

Diego F.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
567 views3 pages

Script V7-Redes Brasil SUPOSTO

Uploaded by

Diego F.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1 TESTE FAILOVER V7

# Define variables
:local Wan1Reference "Primary_WAN" # Reference name for WAN 1 (ether1)
:local Wan2Reference "Failover_LTE" # Reference name for WAN 2 (lte1)
:local Wan1InterfaceName "ether1" # Physical interface name for WAN 1
:local Wan2InterfaceName "lte1" # Physical interface name for WAN 2
:local Wan1CheckHostA "8.8.8.8" # Test host for WAN 1 (Google DNS)
:local Wan2CheckHostA "8.8.4.4" # Test host for WAN 2 (Google DNS)
:local CheckInterval 30 # Check interval in seconds (30 seconds by
default)

# Define routing tables


/routing/table add fib name=("to-".$Wan1Reference)
/routing/table add fib name=("to-".$Wan2Reference)

# Firewall mangle rules for packet marking


/ip firewall mangle rule add chain=prerouting dst-address=!127.0.0.1 table=to-
$Wan1Reference action=mark-packet new-packet-mark="wan1"
/ip firewall mangle rule add chain=prerouting dst-address=!127.0.0.1 table=to-
$Wan2Reference action=mark-packet new-packet-mark="wan2"

# Routing based on packet marking


/ip route add dst=0.0.0.0/0 gateway=$Wan1Gateway table=to-$Wan1Reference mark-
packet="wan1" distance=10
/ip route add dst=0.0.0.0/0 gateway=$Wan2Gateway table=to-$Wan2Reference mark-
packet="wan2" distance=11

# Function to check host connectivity


:function check-host ($interface $host) {
:local result

# Execute ping with count and timeout


$result = [/tool ping get address=$host count=$CheckInterval timeout=1
interface=$interface]

# Check if ping was successful


if ($result > 0) {
:log info "[Failover] $interface: Connected to host $host"
return 1
} else {
:log warning "[Failover] $interface: Failed to connect to host $host"
return 0
}
}

# Function to switch between WAN interfaces


:function switch-wan ($active $inactive) {
# Disable route for inactive interface
/ip route disable [find comment=$inactive]

# Enable route for active interface


/ip route enable [find comment=<span class="math-inline">active\]
\# Update default gateway
/ip route set default gateway\=</span>($active)Gateway

# Log the change


:log info "[Failover] Active WAN interface: $active"
}
# Function to check WAN status
:function check-wan-status () {
# Check connectivity to test host for each interface
:local wan1Status = (check-host $Wan1InterfaceName $Wan1CheckHostA)
:local wan2Status = (check-host $Wan2InterfaceName $Wan2CheckHostA)

# Determine active and inactive interfaces based on status


:local activeInterface ""
:local inactiveInterface ""

if ($wan1Status == 1) {
:set activeInterface $Wan1InterfaceName
:set inactiveInterface $Wan2InterfaceName
} else if ($wan2Status == 1) {
:set activeInterface $Wan2InterfaceName
:set inactiveInterface $Wan1InterfaceName
}

# Handle interface status changes


if ($activeInterface != "") {
# Switch to active interface if different from current active one
if ($activeInterface != [/interface get active name]) {
switch-wan $activeInterface $inactiveInterface
}

# Send email notification if WAN 1 or WAN 2 comes back up


if (($Wan1InterfaceName == $activeInterface) && ($wan1Status == 1) &&
($previousWan1Status == 0)) {
send-wan-up-notification $Wan1InterfaceName $Wan1Gateway
} else if (($Wan2InterfaceName == $activeInterface) && ($wan2Status == 1) &&
($previousWan2Status == 0)) {
send-wan-up-notification $Wan2InterfaceName $Wan2Gateway
}
} else {
# Log if no active interface found

-----------------------------------------------------------------------------------
------------------------------------

2 TESTE FAILOVER V7

Certamente, aqui está um exemplo mais detalhado de um script de failover para


MikroTik RouterOS. Este script assume que você tem duas rotas configuradas, uma
primária e uma secundária, e que você deseja alternar para a rota secundária se a
primária falhar.

Primeiro, configure o Netwatch para monitorar os gateways dos links:

routeros
Copy Code
/tool netwatch
add host=primary_gateway down-script="/failover primary down" up-script="/failover
primary up"
add host=secondary_gateway down-script="/failover secondary down"
up-script="/failover secondary up"
Substitua primary_gateway e secondary_gateway pelos endereços IP dos gateways dos
links que você deseja monitorar.
Em seguida, crie o script de failover:

routeros
Copy Code
/system script
add name=failover policy=ftp,reboot,read,write,test,winbox,password,sniff,sensitive
source={
:local action "\$1";
:local interface "\$2";
:local primaryGateway "primary_gateway";
:local secondaryGateway "secondary_gateway";
:local primaryRouteId;
:local secondaryRouteId;

# Encontre as rotas correspondentes aos gateways


:foreach route in=[/ip route find gateway=$primaryGateway] do={
:set primaryRouteId $route;
}
:foreach route in=[/ip route find gateway=$secondaryGateway] do={
:set secondaryRouteId $route;
}

# Lógica de failover
:if ($action = "primary" && $interface = "down") do={
/ip route set $primaryRouteId disabled=yes;
/ip route set $secondaryRouteId disabled=no;
/log info message="Link primário caiu, alternando para o secundário.";
} else={
:if ($action = "primary" && $interface = "up") do={
/ip route set $primaryRouteId disabled=no;
/log info message="Link primário voltou, habilitando a rota primária.";
} else={
:if ($action = "secondary" && $interface = "down") do={
/log info message="Link secundário caiu.";
} else={
:if ($action = "secondary" && $interface = "up") do={
/log info message="Link secundário voltou.";
}
}
}
}
}
Substitua primary_gateway e secondary_gateway pelos endereços IP dos gateways dos
links que você deseja monitorar.

Este script verifica se o link primário caiu e, se sim, desabilita a rota primária
e habilita a secundária. Quando o link primário volta, a rota primária é habilitada
novamente. Se o link secundário cair ou voltar, uma mensagem é registrada nos logs,
mas nenhuma ação adicional é tomada, pois presumimos que o tráfego já está sendo
direcionado pelo link primário.

Você pode ajustar o script para atender às suas necessidades específicas, como
adicionar mais lógica de failover ou personalizar as mensagens de log.

Lembre-se de que é importante testar o script em um ambiente controlado antes de


colocá-lo em produção para garantir que ele funciona conforme esperado.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy