Community discussions

MikroTik App
 
cyb
just joined
Topic Author
Posts: 11
Joined: Sun Mar 22, 2020 1:29 pm

Enable/Disable wlan with scheduler

Sat Mar 20, 2021 11:48 am

Hi there,

I am trying to build a flexible way to enable/disable wlan at a certain time each day, but not at the same time each day.
For example:
- Enable wlan at 16:00 from Monday to Friday
- Disable wlan at 23:00 from Monday to Friday
- Enable wlan at 08:00 on Saturdays and Sundays
- Disable wlan at 23:59 on Saturdays and Sundays

It is probably the best way to do this with the scheduler, although it is not flexible enough. First problem is estimating the day of week. I found a useful script for that problem here: https://wiki.mikrotik.com/wiki/Script_t ... f_the_week

In the end I want to have multiple scheduler-entries for enabling/disabling the wlans, but those entries should not contain the complete code for estimating the day of week, checking the day of week, enabling the wlan etc. So I tried to use global functions.

I created a script named "Func-DayOfWeek" with the following contents:
:global dayOfWeek do={
#contents from "day of week"-script linked above ending with:
:return $result
}

I created a different script named "Func-EnableWlan" with the following contents:
:global enableWlan do={
  :global dayOfWeek
  :execute [/system script find name="Func-DayOfWeek"]
  :delay 1s
  :local dow [$dayOfWeek]
  :delay 1s
  :log info "scheduler $schedName: checking if $wlanName must be enabled on $dow at $[/system clock get time]"
  :local days [:toarray $dayStr]
  :local match [:find $days $dow]
  :if ([:type $match]!="nil") do={
    :log info "scheduler $schedName: enabling $wlanName"
    :foreach $cap in=[:toarray $capStr] do={
      /caps-man interface enable $cap;
    };
  } else={ :log info "scheduler $schedName: no need to enable $wlanName" }
} on-error={:log error "could not enable wlan"}

I am now trying to create a scheduler entry with the following code:
:global enableWlan
:execute [/system script find name="Func-EnableWlan"]
:[$enableWlan schedName="EnableWlan1" wlanName="wlan1" dayStr="mon,tue,wed,thu,fri" capStr="cap01-2.4-Wlan1,cap01-5-Wlan1"]
Somehow this does not seem to work. Estimating the day of week does work, enabling the wlan does actually work, but I am not able to use the global function enableWlan in the scheduler.

What am I doing wrong?

Is that the best way to handle the problem or is there a better way?

Thanks for your help!

Best regards,
cyb
 
cyb
just joined
Topic Author
Posts: 11
Joined: Sun Mar 22, 2020 1:29 pm

Re: Enable/Disable wlan with scheduler

Tue Mar 23, 2021 10:35 am

No recommendations?

Can somebody tell me whether my understanding of global functions is right?
#This is script "Func-DayOfWeek"

:global dayOfWeek do={
  #Calculate something and put the result to local variable result
  :return $result
}
:global dayOfWeek
:execute [/system script find name="Func-DayOfWeek"]
:delay 1s
:local dow [$dayOfWeek]


Will this lead to the result of the global function being stored in local variable dow?

#This is script "Func-EnableWlan"
:global enableWlan do={
  #Do some actions
  :log info "Parameter 1: $param1" 
}
:global enableWlan
:execute [/system script find name="Func-EnableWlan"]
:[$enableWlan param1="ParamValue"]


Will this trigger doing the actions in "Func-EnableWlan"?
 
cyb
just joined
Topic Author
Posts: 11
Joined: Sun Mar 22, 2020 1:29 pm

Re: Enable/Disable wlan with scheduler

Fri Apr 02, 2021 1:25 pm

Very strange that there is no single reply.

After reading this post about Functions in CMD Scripts I adapted my scripts and it is now working perfectly as I wanted it.

I wanted to have the following feature: the existing wlan MyWLAN should be enabled at the following times:
mon, tue, wed, thu 06:00-08:00 16:30-23:00
fri 06:00-08:00 13:00-23:59
sat 08:00-23:59
sun 08:00-23:00

I wanted to realize this with schedulers. So I created the following schedulers:
EnableMyWLAN-06-00
EnableMyWLAN-08-00
EnableMyWLAN-13-00
EnableMyWLAN-16-30
DisableMyWLAN-08-00
DisableMyWLAN-23-00
DisableMyWLAN-23-59

Each scheduler passes the name of the WLAN, the days when the WLAN should be enabled/disabled and the caps-man interfaces to enable/disable to a global function, e.g. the scheduler-entry for EnableMyWLAN-06-00 looks like:
/system script run "Func-EnableWlan"
:global EnableWlan
:local runFunc [:parse (":local input \"MyWLAN,{mon,tue,wed,thu,fri},{cap01-2.4-MyWLAN,cap01-5-MyWLAN}\"; " . $EnableWlan)]
$runFunc

The scheduler uses the following 3 scripts, which must be created manually:

Func-EnableWlan
# EnableWlan: 	Checks if today is in given array of days where wlan should be enabled
# 				and enables given caps-man interface(s) if so
# Input array:
#	0 = string name of wlan (will just be logged) to enable
#	1 = string comma-concatenated days of week when wlan should be enabled, e.g. "mon,tue,wed,thu,fri"
#	2 = string comma-concatenated names of caps-man interfaces to enable
# Output:
#	nothing
:global EnableWlan ":set input [:toarray \$input]
	:if ([:len \$input] = 3) do={
		:local wlanName [:tostr [:pick \$input 0]]
		:local dayStr [:tostr [:pick \$input 1]]
		:local capStr [:tostr [:pick \$input 2]]

		/system script run \"Func-GetDayOfWeek\"
		:global GetDayOfWeek

		:global dayOfWeek \"\"
		:local runFunc [:parse (\":global dayOfWeek;\" . \
			\$GetDayOfWeek . \
			\":set dayOfWeek \\\$output\")
		]
		\$runFunc
		
		:log info \"checking if wlan \$wlanName must be enabled on \$[:pick [/system clock get time] 0 5]/\$dayOfWeek\"
		:local days [:toarray \$dayStr]
		:local match [:find \$days \$dayOfWeek]
		:if ([:type \$match]!=\"nil\") do={
			:log info \"wlan \$wlanName must be enabled\"
			:foreach cap in=[:toarray \$capStr] do={
				:log info \"now enabling interface \$cap\"
				/caps-man interface enable \$cap;
			};
		} else={ :log info \"no need to enable wlan \$wlanName now\" }
	};"

Func-DisableWlan
# DisableWlan: 	Checks if today is in given array of days where wlan should be disabled
# 				and disables given caps-man interface(s) if so
# Input array:
#	0 = string name of wlan (will just be logged) to disable
#	1 = string comma-concatenated days of week when wlan should be disabled, e.g. "mon,tue,wed,thu,fri"
#	2 = string comma-concatenated names of caps-man interfaces to disable
# Output:
#	nothing
:global DisableWlan ":set input [:toarray \$input]
	:if ([:len \$input] = 3) do={
		:local wlanName [:tostr [:pick \$input 0]]
		:local dayStr [:tostr [:pick \$input 1]]
		:local capStr [:tostr [:pick \$input 2]]

		/system script run \"Func-GetDayOfWeek\"
		:global GetDayOfWeek

		:global dayOfWeek \"\"
		:local runFunc [:parse (\":global dayOfWeek;\" . \
			\$GetDayOfWeek . \
			\":set dayOfWeek \\\$output\")
		]
		\$runFunc
		
		:log info \"checking if wlan \$wlanName must be disabled on \$[:pick [/system clock get time] 0 5]/\$dayOfWeek\"
		:local days [:toarray \$dayStr]
		:local match [:find \$days \$dayOfWeek]
		:if ([:type \$match]!=\"nil\") do={
			:log info \"wlan \$wlanName must be disabled\"
			:foreach cap in=[:toarray \$capStr] do={
				:log info \"now disabling interface \$cap\"
				/caps-man interface disable \$cap;
			};
		} else={ :log info \"no need to disable wlan \$wlanName now\" }
	};"

Func-GetDayOfWeek
# GetDayOfWeek: Estimates day of week of today
# Input:
#	nothing
# Output: 
#	string day of week ("sun", "mon", "tue", "wed", "thu", "fri", "sat")
# original DayOfWeek-script by melboyscout (https://forum.mikrotik.com/viewtopic.php?t=59029#p397102)
:global GetDayOfWeek ":local output \"\"
	:local date [/system clock get date]

	:local months [:toarray \"jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec\"]
	:local daytbl [:toarray \"sun,mon,tue,wed,thu,fri,sat\"]

	:local month [:pick \$date 0 3]
	:local day [:pick \$date 4 6]
	:local dayc [:pick \$date 5 6]
	:local year [:pick \$date 7 11]

	:if ([:pick \$date 4 5] = 0) do={ :set day (\$dayc)}

	:local sum 0
	:local aaa 0
	:local yyy 0
	:local mmm 0
	:local nmonth 1

	:for mindex from=0 to=[:len \$months] do={
		:if ([:pick \$months \$mindex] = \$month) do={:set nmonth (\$mindex + 1) }
	}

	:set aaa ((14 - \$nmonth) / 12)
	:set yyy (\$year - \$aaa)
	:set mmm (\$nmonth + 12 * \$aaa - 2)
	:set sum (7000 + \$day + \$yyy + (\$yyy / 4) - (\$yyy / 100) + (\$yyy / 400) + ((31 * \$mmm) / 12))
	:set sum (\$sum - ((\$sum / 7) * 7))
	:set output [:pick \$daytbl \$sum];"

Maybe someone else is interested. For me it is working quite well. The script code is reduced to a mimimum to avoid duplicate/redundand code. Func-EnableWlan/Func-DisableWlan could be merged to a single function but I didn't like all those ifs to decide between enable/disable.

Best regards,
cyb
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Enable/Disable wlan with scheduler

Fri Apr 02, 2021 6:25 pm

Here is another day of week. https://wiki.mikrotik.com/wiki/Script_t ... f_the_week
Not sure what is the main different from yours, but its not the same.
 
l18oyd
just joined
Posts: 1
Joined: Thu Nov 26, 2020 2:45 pm

Re: Enable/Disable wlan with scheduler

Wed Jul 14, 2021 2:16 pm

Hi - so for completeness - the best way to schedule enabling and disabling WLAN, WLAN2, etc with different times on different days is to paste all scripts into the CMD. For instance, I would like WLAN2 to be turned 'off' / disabled to ensure that people are not using the network then.

I have found the 'IP Parent Control' is a bit more inconsistent as I think with my son's iphone it seems to generate another IP address and he is able to connect that way... Also, if I know we are all asleep, or wanting downtime, there is no device in the household that can access the internet.

Of course, the challenge here may be other connectivity such as CCTV for the house; although this can be hardwired.

Interested to hear how this has been solved simply.

Who is online

Users browsing this forum: No registered users and 19 guests