Hello,
My script is looking for an interface starting with some name, disabling it, and enable a another interface with a random order.
:local activeInterface
:local inactiveInterfaces []
# Gather all WireGuard interfaces starting with "extWG" and find the active one
:foreach interface in [/interface find where name~"^extWG"] do={
:local interfaceName [/interface get $interface name]
:if ([/interface wireguard get $interface running] = true) do={
:set activeInterface $interfaceName
:log info ("Currently active interface: " . $interfaceName)
} else={
:set inactiveInterfaces ($inactiveInterfaces , $interfaceName)
:log info ("Adding inactive interface to the list: " . $interfaceName)
}
}
# Disable the currently active interface
:if ($activeInterface != "") do={
:log info ("Disabling interface: " . $activeInterface)
/interface set [find name=$activeInterface] disabled=yes
:delay 1s
}
# Enable a WireGuard interface from the "inactiveInterfaces" list using a random index
:if ([:len $inactiveInterfaces] > 0) do={
:local headers "Content-Type: text/plain"
:local randomIndex ([/tool fetch url="https://www.random.org/integers//?num=1&min=1&max=18&col=1&base=10&format=plain&rnd=new" http-header-field=$headers as-value output=user ]->"data")
:local randomIndexReal ($randomIndex->"data")
:log info $randomIndex
:log info $inactiveInterfaces
:local interfaceToEnable [:pick $inactiveInterfaces $randomIndexReal]
:do {
:log info ("Enabling interface: " . $interfaceToEnable)
:delay 1s
/interface set [find name=$interfaceToEnable] disabled=no
# Change the out-interface of the NAT rule with number 14 to match the new interface
/ip firewall nat set 14 out-interface=$interfaceToEnable
} on-error={
:log warning "Error enabling interface."
}
} else={
:log warning "No inactive interfaces found to enable."
}
Everything is working quite well but when the script does switch to another interface he does not take care about the random number he got from random.org to select of the 18 wireguard interfaces and instead it selects only the first non-enabled interface.
I must have done something wrong with the :pick function but I’m out of idea now.
Thank you for your help!