Community discussions

MikroTik App
 
User avatar
autominus
just joined
Topic Author
Posts: 17
Joined: Mon Apr 11, 2022 11:18 pm

Script for load balancing

Mon Jul 18, 2022 4:23 pm

Hi,
I prepare configuration for load balancing, but I have a problem with script.
When I send ping to the gateway - script working fine. The gateway is not available and WAN1 is marked by unreachable.
{
	:local rmark "to_wan1"
	:local count [/ip route print count-only where comment="to_wan1"]
	:if ($bound=1) do={
		:if ($count = 0) do={
			/ip route add distance=1 gateway=$"gateway-address" check-gateway=ping routing-mark=to_wan1 comment="to_wan1"
			/ip route add distance=1 gateway=$"gateway-address" check-gateway=ping comment="to_wan1"
			:log warning "WAN1 is direcly connected to the network."
			/tool e-mail send to="***@***" subject="MikroTik $[/system clock get time]" body="WAN1 is direcly connected to the network." start-tls=yes
		} else={
			:if ($count = 1) do={
				:local test [/ip route find where comment="to_wan1"]
				:if ([/ip route get $test gateway] != $"gateway-address") do={
					/ip route set $test gateway=$"gateway-address"
				}
			} else={
				:error "Multiple routes found"
			}
		}
	} else={
		/ip route remove [find comment="to_wan1"]
		log warning "WAN1: the network cable is either faulty or is not connected correctly."
		/tool e-mail send to="***@***" subject="MikroTik $[/system clock get time]" body="WAN1: the network cable is either or is not connected correctly." start-tls=yes
	}
}

But when I want send ping to external host (Google) with the use recursive routing - modified script not working. The host is not available and WAN1 is still marked by reachable.
{
	:local rmark "to_wan1"
	:local count [/ip route print count-only where comment="to_wan1"]
	:if ($bound=1) do={
		:if ($count = 0) do={
			/ip route add distance=1 gateway=$"gateway-address" routing-mark=to_wan1 comment="to_wan1"
			/ip route add distance=1 gateway=$"gateway-address" comment="virtual_to_wan1" dst-address=8.8.8.8/32 scope=10
			/ip route add distance=1 gateway=8.8.8.8 check-gateway=ping comment="virtual_to_wan1"
			:log warning "WAN1 is direcly connected to the network."
			/tool e-mail send to="***@***" subject="MikroTik $[/system clock get time]" body="WAN1 is direcly connected to the network." start-tls=yes
		} else={
			:if ($count = 1) do={
				:local test [/ip route find where comment="to_wan1"]
				:if ([/ip route get $test gateway] != $"gateway-address") do={
					/ip route set $test gateway=$"gateway-address"
				}
			} else={
				:error "Multiple routes found"
			}
		}
	} else={
		/ip route remove [find comment="to_wan1"]
		/ip route remove [find comment="virtual_to_wan1"]
		log warning "WAN1: the network cable is either faulty or is not connected correctly."
		/tool e-mail send to="***@***" subject="MikroTik $[/system clock get time]" body="WAN1: the network cable is either or is not connected correctly." start-tls=yes
	}
}

This script I added to IP/DHCP Client/WAN1.

I don't know what I'm doing wrong.

Who is online

Users browsing this forum: No registered users and 14 guests