Community discussions

 
gammy69er
newbie
Topic Author
Posts: 46
Joined: Sun May 18, 2014 3:01 am

Hotspot Data Limits

Fri Aug 14, 2015 7:08 am

Hey Guys

Have created a New "On Login" Script that will allow for you to give trial Guests any data amount you like, for any period you like. This system allows for complete automation of the HS and does not require a timed script to "check" the HS users - therefore using less resources (presumably) and allowing for direct cut off as soon as data limit has reached.

Firstly, you need a User Profile that is not default. If you have only one Hotspot - you can continue to use this as your persistent profile for the login. if you are using multiple hotspots, it is recommended that you create a separate profile just for the trial login. the profiles will continue to hold you speed limits.

Secondly, you must assign the profile you create with the script below to be your "Trial User Profile" under the Server Profile - any user profiles not set here, or assigned by the script are completely unused and useless

Thridly - you MUST ensure that that profile creates a MAC-Cookie, and that MAC-Cookie Login is Enabled. This will not work as well on ROS 5 (if it even works at all - Cheers Apple for your cookieless logins)

Please note
Blue denotes the Variables that are specific to your Hotspot
Red denotes Variables for data and time
Sorry, would have put them in "Code" Boxes - but html Colours weren't working, and knowing what's adjustable is important.

Below is code for a single hotspot - This goes into the "On Login" Box under "Scripts" of the Hotspot User Profile
:local "hotspot_user" $user;

:if ("$[/ip hotspot user get [find name=$"hotspot_user"] limit-bytes-total]" < "200") do={ /ip hotspot user remove $user }

/ip hotspot user add name="$user" limit-bytes-total=1073741824 limit-uptime=1d server=server1 profile=uprof1 mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"

:if ("$[/ip hotspot active get [find user=$"hotspot_user"] limit-bytes-total]" < "200") do={ /ip hotspot host remove [find mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"]}

End Code


To explain what happens, firstly the button for trial is clicked, this will create a regular trial user, and I recommend that you keep a decent timeout for this user, due to if you make it no time - user will "Admin Logout by timeout" - this means that MAC-Cookie is removed - I recommend keeping it to the same length of time as you have set in your "limit-uptime" in red above.

once this user is created, the on login script Checks if that user has a limit-bytes-total of 200 or less. 200 was an arbitrary number - and essentially anything over 1 should work, due to the fact that Mikrotik doesn't seem to use this data field, and if ROS could use it, I would not have to make this script :P. If the User is less than 200, it will remove the user. If the user has more than 200 limit-bytes-total, then nothing happens (as that would meant that they have already logged in within the limit-uptime period and have been able to click the button again, if required)

then, the system will attempt to create a trial user with the same details as the one that is currently logged in (remember, we have deleted the user above - if applicable - yet they will remain "Active"). if the user already exists, it cannot create another user with the same username, and that will be that. If the user was removed due to lack of "limit-bytes-total", then a new user will be created this the variables you enter - the Blue variables are your Hotspot (so the Server name, and the User Profile), the Red changes your limits for the new user.

PLEASE NOTE - you MUST have "limit-uptime" if you do not add a limit uptime, this will not be handed over from the profile you select for the user, and if there is no limit-uptime, the Hotspot says you have no uptime available (funny that) - especially big issue on Apple devices.

once that is done - the final section will remove the user to allow them to reconnect and login with the correct profile settings. Initially I tried to use "/ip hotspot active remove", but the problem there is that the MAC-Cookie is removed also (any removal of active user request will remove the MAC-Cookie apparently). After finding no information as to how to keep the MAC-Cookie on removal of user - I stumbled across the fact that if you remove the "Host", it will remove any active users associated, then when the host tries to re-connect - MAC-Cookie activates and boom, you are logged back in - as long as your login data is correct.

After that, you have a User, Connected, Logged in and Active - with a Data limit. Once the Data limit is done, Active is Disconnected and "Users" is Updated with the Used amounts of data/time. People will attempt to connect again, and get your login with a "Sorry, trial used", or whatever you have setup there.

After all that, the only thing left to do is setup a Daily/Weekly/Monthly Schedule to wipe out all the old devices clogging up "Users". This is all dependant on how often you would like you limits to be reset. This will also be able to be set in server profile, however the countdown for reset is individual, and if you want a uniform reset time you would use


Below is code for resetting users

/system scheduler
add interval=1d name="Remove Users" on-event=":foreach counter in=[/ip hotspot user find ] do={/ip hotspot user remove start-time=11:00:00

End Code

Again, red denotes Variable - 11 am for this site, and daily .


For Multiple Hotspots - Code is Similar, however due to that fact that you "May" be using different speed/data limits, you may need to have multiple profiles. Obviously this means that you will need to centralise something, and the thing I recommend to centralise if the user Profile. you will have one user profile for each hotspot with the Speeds, etc that you require for each hotspot, and then a single user profile with the following on login script. As you need to enter these profiles as the "Trial User Profile" under the Server Profile to activate them, you will add this central one to each server profile, so that you don't need to script them all.

Below is code for a multiple hotspot setup - This goes into the "On Login" Box under "Scripts" of the Hotspot User Profile


:local "hotspot_user" $user;

:if ("$[/ip hotspot user get [find name=$"hotspot_user"] limit-bytes-total]" < "200") do={ /ip hotspot user remove $user }

/ip hotspot user add name=$"hotspot_user" limit-bytes-total=1073741824 limit-uptime=1d server=server1 profile=uprof1 mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"
/ip hotspot user add name=$"hotspot_user" limit-bytes-total=5368709120 limit-uptime=1d server=server2 profile=uprof2 mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"
/ip hotspot user add name=$"hotspot_user" limit-bytes-total=10737418240 limit-uptime=1d server=server3 profile=uprof3 mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"

:if ("$[/ip hotspot active get [find user=$"hotspot_user"] limit-bytes-total]" < "200") do={ /ip hotspot host remove [find mac-address="$[/ip hotspot active get [find user=$"hotspot_user"] mac-address]"]}

End Code

here you can see that I have 3 hotspots, 1 with 1gb, 1 with 5gb and 1 with 10gb.

speed limits are assigned via the user profiles as follows

uprof1 384k/5M 1M/10M 256k/2M 20/20 6

uprof2 1M/5M 2M/10M 768k/3M 20/20 5

uprof3 2M/10M 5M/20M 1536k/7M 20/20 4

This allows for preferred guests to get better service. These 3 Hotspots are separated by 3 Wireless SSID's, each with a WPA2 Key - so only verified guests can access.

again, all those are variable and can be adjusted to suit your network.

Just going over all this again has Fried my shrivelled up Brain, it took me over a month to get this script useable, and 3 months to debug - but have had over a month now where there are no reported issues as to how it runs.

Will leave it at that for the moment - will attempt to post in Forum where people have asked for this request as a potentially better way than the "official" way of scanning "Actives" every 5-10 mins.

If anyone has a query/comment - please feel free to post it
 
gammy69er
newbie
Topic Author
Posts: 46
Joined: Sun May 18, 2014 3:01 am

Re: Hotspot Data Limits

Mon Nov 30, 2015 2:04 am

Just an update - Had to Replace the Router - unrelated Issue - But had to rebuild this script. have just realised that i have sent you all down the garden path in regards to the Removal Of Users Script - it is missing a Key component due to the fact that the ROS cannot Support the way this command is written in terminal - becaus the scrip requires a special character with negates the script being completed (and also kills the export - apparently...)

Here is the Full Script

Begin Code

/system scheduler
add interval=1d name="Remove Users" on-event=":foreach counter in=[/ip hotspot user find] do={/ip hotspot use
r remove $counter }" start-time=11:00:00

End Code

Now, the $counter in the Codes is what breaks it as a Terminal Command (and a terminal export). it is required in the script and appears that it must be added manually. Once in the script - runs Well. I would recommend either building the schedule yourself and pasting this script

Begin Code

:foreach counter in=[/ip hotspot user find] do={/ip hotspot user remove $counter }

End Code


Again, sorry for not checking what was being copied and pasted - i'm sure that most of you who have used this will have figured this out by now though :)
 
gammy69er
newbie
Topic Author
Posts: 46
Joined: Sun May 18, 2014 3:01 am

Re: Hotspot Data Limits

Fri Jul 22, 2016 9:53 am

Sooooo... I see Mikrotik have added a Data limit thing in the Latest Versions of ROS - YAY

However it is still lacking one key feature - Removal of accounts @ Time of Day e.g. no matter what time someone logs on, all data reset @ say 9 am.

Also, in implementing this - they have broken my Script :( ... well the removal of users script.

Here is the update
/system scheduler

add interval=1d name="Remove Users" on-event=":foreach counter in=[/ip hotspot user find] do={:if (\$counter != \"*0\")  do={/ip hotspot user remove \$counter}}" policy=read,write,policy,test,sniff,sensitive start-date=dec/10/2014 start-time=06:00:00
The problem is that a default "User" is added, and cannot be removed.  Eventually I found the string that it showed as in the Router (hence the != "*0") and managed to run the remove command without touching the untouchable default user.

Admittedly, the new feature looks nice (and is about time) - however having the "Profile" settings under the "User" list is a little bit dickey, but at least they are getting there... Please don't break my scripts next time though :P
 
dinho
just joined
Posts: 1
Joined: Tue Aug 02, 2016 5:09 pm

Re: Hotspot Data Limits

Thu Aug 04, 2016 4:12 pm

Hello Gammy69er,

First I need to commend on your efforts with this script and also for sharing. Good Job!

I'll also like to make known the fact that I am new to scripting and RouterOS, so please bear with me.

Although everything works fine, I have a little problem, I discovered that as soon as a user reaches his/her daily limit, any other users that attempts to log on to the hotspot afterwards are told they have reached their daily limit. but the ones who already logged on to the hotspot before any user reach his limit would still be able to use the hotspot until they reach their limit.

I think its a bug with the script, or maybe there is something I am not doing right.

Please Help!!! I have so much pressure on me because the organization am working for need to start rolling out these routers.

Thank you.
 
nukman5000
just joined
Posts: 1
Joined: Thu Aug 29, 2019 10:13 am

Re: Hotspot Data Limits

Fri Aug 30, 2019 4:57 pm

Thanks for your code. I would like to know how to modify it a bit though. I have setup hotspot and its working perfectly. My ISP gives me a monthly allocation of 100GB daytime data(6am-11pm) and 100GB night-time data(11pm-6am). I want to allocate this data over 30 days meaning each day I will allocate 1,700mb(day) and 1,700mb(night). I then want to split this data between 2 users so each gets 850mb(day) and 850mb(night). How can I go about this using a script. I am using an HAP LITE and unfortunately, it doesn't come with userman.

Who is online

Users browsing this forum: No registered users and 2 guests