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