MikroTik: How to disable (enable) the interface on a schedule

We will analyze several ways to disable (enable) the MikroTik network interface or disable the Internet, according to a schedule: Firewall rule, NAT, MikroTik task scheduler or MikroTik script.

Content

  1. Firewall rule: how to disable (enable) an interface on a schedule
  2. NAT: how to allow (deny) the Internet on a schedule
  3. Scheduler: disable (enable) interface by time
  4. MikroTik script: how to disable (enable) the interface

Firewall rule: how to disable (enable) an interface on a schedule

To disable or disable the interface on a schedule, you can use the Firewall rule setting on the Extra tab.

⚠️ For the rule to work, the interface must be the main interface.

For example: we will restrict the operation of the WiFi interface (wlan2-5GHz) during non-working hours, from 19:00 to 07:00.

Create a rule:

[IP] -> [Firewall] -> [Filter Rules] -> [+] -> [General: Chain=forward, In. interface=wlan2-5GHz; Extra: Time=19:00:00-07:00:00; Comment="Blocking traffic on the interface, according to the schedule."]

in terminal:

/ip firewall filter add action=reject chain=forward in-interface=wlan2-5GHz reject-with=icmp-network-unreachable time=9h-7h,sun,mon,tue,wed,thu,fri,sat comment="Blocking traffic on the interface, according to the schedule."

If you want to disable the operation of the interface on Saturday and Sunday (full day), set the time values:

[IP] -> [Firewall] -> [Filter Rules] -> [+] -> [General: Chain=forward, In. interface=wlan2-5GHz; Extra: Time=00:00:00-1d 00:00:00, Days=sun, sat; Comment="Blocking traffic on the interface, according to the schedule."]

in terminal:

/ip firewall filter add action=drop chain=forward in-interface=wlan2-5GHz time=0s-1d,sun,sat comment="Blocking traffic on the interface, according to the schedule."
MikroTik Firewall Rule: How to disable (enable) an interface on a schedule

NAT: how to allow (deny) the Internet on a schedule

You can deny (or allow) access to the Internet by time using the masquerade (NAT) rule setting.

Edit the masquerade rule, under the Extra tab:

[IP] -> [Firewall] -> [NAT] -> [Extra: Time=19:00:00-07:00:00, Days: mon,tue,wed,thu,fri; Comment="Rule runs on schedule."]

in terminal:

/ip firewall nat add action=masquerade chain=srcnat out-interface-list=ExternalInterfaces time=8h-20h,mon,tue,wed,thu,fri comment="Rule runs on schedule."
NAT: How to allow (deny) the Internet on a schedule

Scheduler: disable (enable) interface by time

Turn off WiFi wlan2-5GHz interface after 20:00:00 using MikroTik Scheduler:

[System] -> [Schedule] -> [+] -> [Name: "Interface: Disable WiFi"; Start Time: 20:00:00; Interval: 1d 00:00:00; Policy: read, write, policy, test; text: "[/interface set wlan2-5GHz disabled=yes]"; Comment: Disable the interface at the specified time]

in terminal:

/system scheduler add name="Interface: Disable WiFi" start-date=feb/22/2021 start-time=20:00:00 interval=1d on-event="[/interface set wlan2-5GHz disabled=yes]" policy=read,write,policy,test comment="Disable the interface at the specified time"

Enable WiFi wlan2-5GHz interface from 08:00:00 using MikroTik Scheduler:

[System] -> [Schedule] -> [+] -> [Name: "Interface: Enable WiFi"; Start Time: 20:00:00; Interval: 1d 00:00:00; Policy: read, write, policy, test; text: "[/interface set wlan2-5GHz disabled=no]"; Comment: Disable the interface at the specified time]

in terminal:

/system scheduler add name="Interface: Enable WiFi" start-date=feb/22/2021 start-time=08:00:00 interval=1d on-event="[/interface set wlan2-5GHz disabled=no]" policy=read,write,policy,test comment="Enable the interface at the specified time"
MikroTik Scheduler: Turn off (turn on) the interface by time

MikroTik script: how to disable (enable) the interface

You can also disable (or enable) the interface using the MikroTik script and the Task Scheduler.

Create script

Specify the following variables in the script:

  1. InterfaceName – interface name (in the example wlan2-5GHz);
  2. StartEnableTime – time, from which the interface should be turned on (in the example 08:00:00);
  3. EndEnableTime – time, after which the interface should be turned off (in the example 20:00:00).
[System] -> [Scripts] -> [+] -> [Name: WorkTimeInterface] -> [Policy: read, write, test, policy]

Script code:

# Name: WorkTimeInterface v1
# Description: Enable or disable the selected interface on a schedule.
# Author: Yun Sergey [MHelp.pro] © 2021
# License: GPL-3.0 License
# Description, purpose and questions: https://mhelp.pro/mikrotik-how-to-disable-enable-the-interface-on-a-schedule/
# More scripts Mikrotik: https://mhelp.pro/tag/mikrotik-scripts/
# Verified: RouterBOARD 952Ui-5ac2nD, RouterOS 6.48.1 (stable)

:local InterfaceName wlan2-5GHz;
:local StartEnableTime [:totime "08:00:00"];
:local EndEnableTime [:totime "20:00:00"];

:local StatusInterface [/interface get $InterfaceName disabled];
:local CurrenTime [/system clock get time];

#:log info "Script WorkTimeInterface running. Interface $InterfaceName disabled: $StatusInterface.";

:if ( $StartEnableTime < $CurrenTime and $CurrenTime < $EndEnableTime) do={
    #:log info "Script WorkTimeInterface: work time.";
    :if  ($StatusInterface) do={
        [/interface set $InterfaceName disabled=no];
        :log info "Script WorkTimeInterface: interface $InterfaceName was disabled, enabled the interface.";
    } else={
        #:log info "Script WorkTimeInterface: skip enable interface, $InterfaceName already enabled.";
    };

} else={
    #:log info "Script WorkTimeInterface: not work time";
    :if  (!$StatusInterface) do={
        [/interface set $InterfaceName disabled=yes];
        :log info "Script WorkTimeInterface: interface $InterfaceName was enabled, disabled the interface."
    }
}
#:log info "Script WorkTimeInterface finished. Interface $InterfaceName disabled: $StatusInterface.";

If the script does not work or does not work correctly, uncomment the lines :log (remove the # symbol in front of the beginning of the line) and check the MikroTik log when the script starts.

Add script to Scheduler

To run the script, you need permission: read, write, test, policy.

[System] -> [Schedule] -> [+] -> [Name: "Interface: Enable/Disable WiFi"; Start Time: 00:00:50; Interval: 00:15:00; Policy: read, write, policy, test; text: "[/system script run WorkTimeInterface]"; Comment: "Run script Enable/Disable interface"]
MikroTik Script: How to disable (enable) the interface

? How to disable (enable) the MikroTik interface on a schedule or allow (deny) access to the Internet by time was discussed in this article. I hope you have chosen a suitable way to manage the state of the interface. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.48.1 (stable).

Leave a Comment