Ikki WAN uchun avtomatik failover skripti

⏱ O‘qish vaqti: 9 daqiqa

Qisqacha

  • Skript ikki interfeysni ping qilib ulanish holatini tekshiradi.
  • Belgilangan ping xatoliklar soniga yetganda, muammoli gateway uchun default marshrut masofasini oshiradi.
  • Masofa oshirilgan marshrut ikkinchi ISP ga yo‘naltiriladi, shu bilan avtomatik failover amalga oshadi.

Ko‘p ISP’ga ega bo‘lgan tarmoqlarda birinchi ulanish uzilganda trafik ikkinchisiga o‘tishi kerak. MikroTik’da bu vazifani qo‘lda sozlash murakkab bo‘lishi mumkin. Quyidagi skript ping asosida avtomatik failoverni ta’minlaydi va yo‘llarning masofasini dinamik o‘zgartiradi.

Talablar

  • RouterOS 6 yoki undan yuqori versiya
  • Ikki WAN interfeysi (masalan ether1, ether2) mavjud
  • Administrator huquqi
  • Ping target IP (masalan 208.67.222.222)

Sozlash bosqichlari

1. Skriptni sozlash

Skript boshida :local InterfaceISP1 ether1, :local InterfaceISP2 ether2 kabi o‘zgaruvchilar mavjud. O‘zingizning WAN interfeys nomlari, gateway IP‑lari, ping target va xatolik chegarasini (FailTreshold) hamda masofa oshirish qiymatini (DistanceIncrease) kiriting.

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

# ------------------- 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 ether2
# Please fill the gateway IPs (or interface names in case of PPP)
:local GatewayISP1 1.1.1.1
:local GatewayISP2 2.2.2.2
# 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}
# 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=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}
			: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=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			: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=$GatewayISP2 && 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=$GatewayISP2 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			: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.

Credit: wiki.mikrotik.com

2. Global o‘zgaruvchilarni e’lon qilish

Skript :global PingFailCountISP1 va :global PingFailCountISP2 ni e’lon qiladi, keyin ularning qiymatini 0 ga boshlaydi, shunda birinchi ishga tushirishda hisoblagichlar mavjud bo‘ladi.

3. Ping tekshiruvi va marshrutni o‘zgartirish

Har bir interfeys uchun ping $PingTarget count=1 interface=$InterfaceISP1 bajariladi. Natija 0 bo‘lsa, hisoblagich oshiriladi; hisoblagich belgilangan FailTreshold dan oshsa, muammoli interfeysga tegishli default marshrutning distance qiymati DistanceIncrease ga oshiriladi, natijada bu marshrut ikkinchi yo‘nalishga aylanadi.

Tekshirish

Skript har bir ping natijasini :put $PingResult orqali terminalga chiqaradi; muvaffaqiyatsiz pinglar va marshrut masofasining oshirilganini ko‘rish orqali failover ishlashini tasdiqlang.

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

# ------------------- 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 ether2
# Please fill the gateway IPs (or interface names in case of PPP)
:local GatewayISP1 1.1.1.1
:local GatewayISP2 2.2.2.2
# 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}
# 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=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] + $DistanceIncrease)}
			: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=$GatewayISP1 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			: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=$GatewayISP2 && 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=$GatewayISP2 && static] do=\
				{/ip route set $i distance=([/ip route get $i distance] - $DistanceIncrease)}
			: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.

Credit: wiki.mikrotik.com

Diqqat:

  • Interfeys nomlari va gateway IP‑larini aniq kiriting, aks holda skript noto‘g‘ri yo‘llarni o‘zgartirishi mumkin.
  • DistanceIncrease qiymati juda katta bo‘lsa, tarmoqda noaniq marshrutlash paydo bo‘lishi ehtimoli bor.
  • Skript faqat ikki ulanish uchun mo‘ljallangan; uchinchi yoki ko‘p ISP qo‘shilsa, qo‘lda o‘zgartirish talab etiladi.

Muammolar va yechimlar

Ping natijasi har doim 0 chiqadi

Gateway IP yoki PingTarget manzilini tekshiring, u Internetga erkin ping qilishi kerak.

Marshrut masofasi oshmayapti

FailTreshold va DistanceIncrease qiymatlarini qayta ko‘rib chiqing, va scriptni qayta ishga tushiring.

Xulosa

Bu skript ikki WAN ulanishini monitoring qilib, muammoli ulanish uzilganda avtomatik ravishda ikkinchisiga yo‘naltiradi. To‘g‘ri sozlanganida tarmoq uzilishlariga qarshi mustahkam yechim bo‘ladi.

Manba: Failover With Scripting — mikrotiktool.github.io

0 0 ovozlar
Article Rating
Obuna boʻling
Xabar berish
guest

0 Comments
Eng avvalgi
Eng yangi Eng ko'p ovoz berilgan