Create a script

I’m new to MikroTik scripts and have received a lot of help with the scripts you guys have created. I want to create a script to delete a user in a hotspot when that user has used up their usage time. The user’s expiration time would be written in the comment.

Thank you for your valuable collaboration.

With the help of the forum, I was able to create this script, and it works perfectly. Now I need to create a script that deletes online users when their date and time expire. Can anyone give me a clue on how to solve this? Thanks.

:global futuredatetimestr do={
[...]
}
{
:local date [/system clock get date]
:local time [/system clock get time]
:local comment [/ip hotspot user get [find where name=$user] comment]
:local ucode [:pick $comment 0 2]

:if ($ucode = "vc" or $ucode = "up" or $comment = "") do={

  :local limitUptime [/ip hotspot user get [find where name=$user] limit-uptime]
  :if ([/system scheduler find where name=$user] != "") do={
      /system scheduler remove [find where name=$user]
  }
  /system scheduler add name=$user start-date=$date start-time=$time interval=$limitUptime on-event=":return"
}

:delay 2s

:local newInterval [/system scheduler get [find where name=$user] interval]
:local nsd [$futuredatetimestr $newInterval]
:local startDate [:pick $nsd  0  12]
:local startTime [:pick $nsd 12 21]
:local Date [/system clock get date]
:local Time [/system clock get time]
/system scheduler remove [find where name=$user]
:local comment "IN USE FROM: $Date$Time ==> TO: $startDate$startTime"
:local userprofile [ip hotspot user get [find where name="$user"] profile]
:if ([/ip hotspot user get $user comment]="") do={/ip hotspot user set $user comment="$comment"}
 /system scheduler remove [find where name=$user]
}

Cannot help with your script, but (general advice) you should NEVER use for your :local or :global variables a name same as a Router Os settings, property or command, these are “reserved”.

Use - say - mydate or thisdate instead of date.

The trouble with using same name is that It doesn’t generate an error, but results might be unpredictable, or work in one script and fail in another one.

Also, variables, file names, etc., are CaSe SeNsItIvE, date is not the same as Date.

I managed to write the script, friends. Thanks for the help.

Since I’m not an expert in scripts, could someone please correct this?

You don’t say what’s failing, what version, nor include all the code. And, not even where it being run from.

One technique is to add /log info "something" lines to help debug the variables values and confirm your logic. That might give you more clues at to what’s going on.

And to @jaclaz points, add a :local currentUser $user to top of script then use $currentUser in the [find] and other code – not $user.

Thanks for your help Amm0. The version is 6 and this is the script:

:local dateint do={
:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )
:local days       [:pick $d 4  6]
:local month      [:pick $d 0  3]
:local year       [:pick $d 7 11]
:local monthint   ([:find $montharray $month])
:local month      ($monthint + 1)
:if ( [len $month] = 1) do={
:local zero ("0")
:return [:tonum ("$year$zero$month$days")]} else={:return [:tonum ("$year$month$days")]
        }
       }

:local timeint do={
:local hour       [:pick $t 0 2]
:local minutes    [:pick $t 3 5]
:return ($hour * 60 + $minutes)}
:local mydate     [/system clock get date]
:local mytime     [/system clock get time]
:local today      [$dateint d=$mydate]
:local curtime    [$timeint t=$mytime]
:foreach i in     [ /ip hotspot user find where ] do={
:local mycomment  [/ip hotspot user get $i comment]
:local myname     [/ip hotspot user get $i name]
:local getdate    [:pick $mycomment 52 63]
:local gettime    [:pick $mycomment 64 72]
:if ([:pick $mycomment 55] = "/" and [:pick $mycomment 58] = "/") do={
:local expd       [$dateint d=$getdate]
:local expt       [$timeint t=$gettime]
:if (($expd < $today and $expt < $curtime) or ($expd < $today and $expt > $curtime) or ($expd = $today and $expt < $curtime)) do={
 [/ip hotspot user remove $i]
 [/ip hotspot cookie remove [find where user=$myname]]
 [/ip hotspot active remove [find where user=$myname]]
        }
       }
      }

The date format changed in 7.10, see:

from (example) apr/28/2024 to 2024-04-28
so you need to parse it differently (and you need not anymore the montharray):

I’m not spam use:

:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )

Because as you can see in the graphic the date is all in “lowercase”, that’s why I use it.

I hope you understand me and can correct my script, if it needs correcting. Thank you.

Ah, OK, so you have the OLD date format.

WHAT does not work in that script?

The two functions dateint and timeint look fine at first sight.

All the rest is conditional on the comment the found hotspot users have, since everything is picked with hardcoded offset and length, any mistake in the comment formatting or contents is likely to make the script do nothing.

You need to add a series of :put commands to check what values all variables get when run.

OP using version 6 AFAIK. So date should be fine. Unless the issue “porting” it RouterOS v7.

The problem here is OP has not stated what the problem, nor added any kinda of logging that narrow down whatever the issue is.

Otherwise the script looks “fine” since it unclear what issue to even look for in it.

Thanks for your responses, friends.
One last question. Why, if I put the script in a scheduler and tell it to run every 10 seconds, doesn’t it? It runs every 1 minute?
How do I close the post?