Like probably most of the MikroTik users willing to learn scripting, I have exercised myself with a script dedicated to toggling wifi on/off through pressing the Mode button (Router OS v7.20.6). I have tested it and it works for me. I consider that wifi is on if at least one interface is active.
Since it was made in order to learn, I am open to any constructive criticism
Activation_désactivation_wifi
:local Wifi1Inactivity [/interface get wifi1 disabled]
:local Wifi2Inactivity [/interface get wifi2 disabled]
:if ($Wifi1Inactivity = true and $Wifi2Inactivity = true) do={
/interface wifi enable wifi1
/interface wifi enable wifi2
:log info "Activation du wifi"
} else={
/interface wifi disable wifi1
/interface wifi disable wifi2
:log info "Désactivation du wifi"
}
I assigned it to the Mode button through the GUI.
EDIT : I have another wifi interface dedicated to iot, but it doesn’t seem to be autonomous, probably because it is a slave of wifi2.
Since you're merging the state of all interfaces, you might want to store it in a shared variable, like this:
:local AllWifiInactivity ([/interface get wifi2 disabled] and [/interface get wifi1 disabled]) or no variables at all
:if ([/interface get wifi1 disabled] and [/interface get wifi2 disabled]) do={
You can enable and disable it with one line
/interface wireless enable wifi1,wifi2
Virtual interfaces can be controlled by number
interface/wireless/enable numbers=3,4
My commands use wireless because I'm using that package
One addition you could do is use a [find ssid="yourSSID2Toggle"], for example, instead of referencing the interface name directly. The general reason being is if you find in scripting, you can abstract things like interface name so you’re “safe” from folks renaming the interfaces…
:if ([/interface get wifi1 disabled] and [/interface get wifi2 disabled]) do={
/interface wifi enable [/interface wifi find configuration.ssid="MikroTik-xx"]
:log info "Activation du wifi"
} else={
/interface wifi disable [/interface wifi find configuration.ssid="MikroTik-xx"]
:log info "Désactivation du wifi"
}
EDIT : I realise that I should have avoided hard-scripting wifi1 and wifi2 in the condition. I suppose I might have used a for loop. I’ll try and conceive it.
Yep, but the link I posted was intended to let you know about the clever trick by rextended, your script checks if something is disabled and if it is, turns it on, or, if it is enabled, and then turns it off.
The linked approach works (IMHO more directly) with the opposite of the status found:
Thanks for that precision. I didn’t understand initially.
I will think about it but I’m not sure it fits my case, since there are 2 wifi interfaces and I want to deactivate both even if only one is active, or reactivate them both if none is active.
This script monitors the state of button presses and is not connected to the state of WiFi. WiFi can be changed programmatically, and then the logic of button presses will be reversed.