Hello,
I’m very new to Mikrotik scripting (and already hate the extremely bad syntax and lack of error reporting!). I have four WAN links with four DHCP clients, first one with a default route and the others without. I’m trying to use a DHCP lease script on the 3 DHCP clients without a default route to set routing marks and rules. I’ve based my script off the example from https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Client#Lease_script_example and it seemed to work fine on WAN4. I copied it to WAN3 and updated the routing mark name and released the DHCP client to get it to to update. When I did this, the script deleted the default route added by WAN1 and I lost all access to the router.
Any idea what went wrong?
{
:local routingmark "WAN3"
:local count [/ip route print count-only where comment=$routingmark]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway="$"gateway-address"%$"interface"" pref-src=$"lease-address" comment=$routingmark routing-mark=$routingmark
} else={
:if ($count = 1) do={
:local test [/ip route find where comment=$routingmark]
:if ([/ip route get $test gateway] != $"gateway-address") do={
/ip route set $test gateway="$"gateway-address"%$"interface"" pref-src=$"lease-address"
}
} else={
:error "Multiple routes found"
}
}
:local count2 [/ip route rule print count-only where comment=$routingmark]
:if ($count = 0) do={
/ip route rule add src-address=$"lease-address" table=$routingmark comment=$routingmark
} else={
:local test2 [/ip route find where comment=$routingmark]
/ip route rule set $test2 src-address=$"lease-address"
}
} else={
/ip route remove [find comment=$routingmark]
/ip route rule remove [find comment=$routingmark]
}
}