Hi please i have been trying to configure my hotspot such that some user name could only be use at night time and be disable during the day say from 8am till 6pm and will be active from 6pm through the night till 7am in the morning
Thanks in advances for all your contribution and help.
You can use simple queue with time interval setting.
One rule with the 8am - 6pm restriction (1k/1k) and a second rule permitting normal browsing between 6pm and 7am.
However you can only apply the queue to a specific ip or subnet and not to a user; so if you use static IPs it’s straightforward but if you use dhcp, then you will need to do ip binding in hotspot.
OR
Here’s a wiki alternative based on scripting and scheduling.
http://wiki.mikrotik.com/wiki/Limit_Different_Bandwidth_In_Day_and_Night
For 8am - 6pm you can limit-at 1k/1k.
Again, you can only apply it to ip addresses.
Write a script that goes through the existing users and disables those that match a certain condition (for example, configure all users to be disabled on a schedule to have a certain string in their comment field). Have that script also remove the active sessions for those users. Write a second script that enables those accounts. Use the system scheduler to fire the scripts at the times you want them wih a 24 hour delay between runs.
HTH,
Felix
All right now please can u help me with one because am not good in writing script. our if there is a material that explain that very well i will appreciate.
Thanks
The below scripts assume that all users to be on a limited schedule to have a comment field exactly equal to “Schedule limited user”:
/ip hotspot user set [/ip hotspot user find name=test] comment="Schedule limited user"
Script #1 disables all users that have such a comment, name it disableScheduleLimitedUsers:
# find all users that need to be disabled
:foreach user in=[/ip hotspot user find comment="Schedule limited user"] do={
# remove all active user sessions for that name
:local name [/ip hotspot user get $user name];
:foreach activeuser in=[/ip hotspot active find name=$name] do={
/ip hotspot active remove $activeuser
}
# set the user to disabled
/ip hotspot user disable $user
}
Script #2 re-enables them, name it enableScheduleLimitedUsers:
# find all users that need to be enabled
:foreach user in=[/ip hotspot user find comment="Schedule limited user"] do={
# enable the user
/ip hotspot user enable $user
}
Add two scheduled jobs, one running at 7am every day disabling all the users, another one running at 8pm every day enabling all the users:
/system scheduler add name=disableScheduleLimitedUsers on-event=disableScheduleLimitedUsers start-date=jan/01/2009 start-time=07:00:00 interval=24h
/system scheduler add name=enableScheduleLimitedUsers on-event=enableScheduleLimitedUsers start-date=jan/01/2009 start-time=20:00:00 interval=24h
That code compiles though I haven’t had a chance to properly test it yet. Should get you going, though.
HTH,
Felix
Edit: Tested and works on 3.28
Thanks a lot am almost getting there, please i may still have to border you the names i want to disable were created with userman.
If the Usermanager instance and the hotspot itself run on the same machine, adjusting the scripts like so:
# find all users that need to be disabled
:foreach user in=[/tool user-manager user find comment="Schedule limited user"] do={
# remove all active user sessions for that name
:local name [/tool user-manager user get $user name];
:foreach activeuser in=[/ip hotspot active find name=$name] do={
/ip hotspot active remove $activeuser
}
# set the user to disabled
/tool user-manager user disable $user
}
# find all users that need to be enabled
:foreach user in=[/tool user-manager user find comment="Schedule limited user"] do={
# enable the user
/tool user-manager user enable $user
}
might work. It will of course not work if the hotspot is on a different machine than the Usermanager. I can’t test this scenario as I don’t use Usermanager.
Hope it works,
Felix
All work great thanks a million i use it with some other variable
code 1
find all users that need to be disabled
:foreach user in=[/tool user-manager user find comment="night"] do={
remove all active user sessions for that name
:local name [/tool user-manager user get $user name];
:foreach activeuser in=[/ip hotspot active find name=$name] do={
/ip hotspot active remove $activeuser
}
set the user to disabled
/tool user-manager user disable $user
}
code 2
find all users that need to be enabled
:foreach user in=[/tool user-manager user find group-name=night] do={
enable the user
/tool user-manager user enable $user
}
i then create a profile for night users call night on /ip hotspot user profile add name=night
and from userman when createing the user on the group field i enter night
and every thing work well,i also see one could use some other variables that are there too.
once more thanks
Just to make sure:
You’ll want to change
:foreach user in=[/tool user-manager user find comment="night"] do={
in the first script to
:foreach user in=[/tool user-manager user find group-name="night"] do={
like you did in the second script.
I’m glad it’s working.
Felix
Yea i did the second one but i also try with the first they bolt work fine and some other variable but i prefer group-name
were can i learn more about the user of those command like
code
:foreach user ,do={:local name,$user name, ;" what they mean and how to use them "
do={,
I hope am not asking too many questions
Regards Onowojemma.
The wiki has a section on scripting.
Hello Fewi,
I just upgraded from v 2.9.27 to 4.5 and tried to use the script. They worked fine. Great Job.
Cheers!
Fewi,
You are extremely powerful as far as Miktotik is concerned. Will it be possible to have ur private mail add so dat i can PM you incase i run into some configurations issues.
How can i assign a server/mirotik that hass about 3 cafes, using about 10pcs each to share this bandwidth 98kbps/196kbps (Upload/Downlaod) so that all of my lan/hotspot users will share that bandwidth alone.
My plan is to make sure that other bandwidth from my ISP is shared where, tha above will be for those on the hotspot alone whilke others will be for other servers housing my office.
This script is great!
But can UM developer help us make it available in GUI form? for more flexibility like configuring some user to browse for a maximum of 2hours in a day and so on.
I need this but going by script is cumbersome as I have plenty script running already and very soon everything will be confusing and difficult to trouble shoot.
Thanks for your effort and time.
This script is nice, but has a minor problem:
when it runs it will disconnect ALL users that are currently connected to hotspot and all users have to relogin.
Is possible to find a way that will remove only the matched users from the active list?!
Thank you
# find all users that need to be disabled
:foreach user in=[/tool user-manager user find comment="Schedule limited user"] do={
# remove all active user sessions for that name
Re
:foreach activeuser in=[/ip hotspot active find name=$name] do={
/ip hotspot active remove $activeuser
}
# set the user to disabled
/tool user-manager user disable $user
}
replace the 5th line:
:foreach activeuser in=[/ip hotspot active find name=$user] do={
This will not disconnect all connected users , will disconnect only the users matched!
I don’t think that would work. $user is a reference to the actual user object and basically equals an id, not a name (a string).
I don’t see how the original code logs out all connected users. It first finds all user objects that have a specific comment set, then gets the name of that user object, and then logs off all active sessions logged in under that name.
2nd line: :foreach user : $user list include all users id with the specific comment,isnt it?
5th line , name=$name : this actually match all users, correct?
I have checked my script three times, and always worked disconnecting only the correct client and disabling the correct usernames! ![]()
Is there a way to match users according to their name, and based on a pattern or regular expression?
Mikrotik has released a manual explaining all this macros?
You could do this a couple ways.
1 - You could include the names in the script itself:
:local users "user1,user2,user3"
/ip hotspot active {
:foreach item in=[find] do={
:foreach u in=[:toarray $users] do={
:if ([get $item user] = [:tostr $u]) do={
# user name found, perform action here
:put ("Found active user: " . [get $item user])
}
}
}
# /ip hotspot active
}
2 - Search for name using regular expression (version >= 3.23):
:local userregex "<user regex string>"
/ip hotspot active {
:foreach item in=[find] do={
:if ([get $item user] ~ [:tostr $userregex]) do={
# user name found, perform action here
:put ("Found active user: " . [get $item user])
}
}
# /ip hotspot active
}
A great article about regular expression syntax can be found here: http://en.wikipedia.org/wiki/Regular_expression#POSIX_.28Portable_Operating_System_Interface_.5Bfor_Unix.5D.29
I am not searching a tutorial for regex but for mikrotik scripting language! (I dont know how they named it)
thx