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.
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.
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.
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.
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?