This script comes from the final slide of my recent MUM presentation on RouterOS scripting
It allows you to assign a data limit to trial hotspot users and:
a) have them kicked offline upon reaching this limit
b) create a temporary user to stop them from being able to log back in again
This is a feature which is not available via the current hotspot settings, so I decided to script it into existence. Enjoy!
And How (scripts can be fun ya see?):
Script to check users for those over the limit:
Make a scheduler entry to run this every X minutes, I’d recommend every 5-10 minutes.
#Download limit in MB :local downquotamb 50 ### Do not modify anything below this line ### :local downquota [$downquotamb * 1000 * 1000] :local counter :local datadown :local username :local macaddress :foreach counter in=[/ip hotspot active find ] do={ :set datadown [/ip hotspot active get $counter bytes-out] :if ($datadown>$downquota) do={ :set username [/ip hotspot active get $counter user] :set macaddress [/ip hotspot active get $counter mac-address] /ip hotspot user remove [/ip hotspot user find where name=$username] :if ( $username=”T-$macaddress”) do={ /ip hotspot user add name=$username limit-bytes-out=$downquota mac-address=$macaddress /ip hotspot active remove $counter } :log info "Logged out $username - Reached download quota" }}
Script to clear all manually created user accounts:
This can be done hourly, daily or weekly depending on your requirements.
:foreach counter in=[/ip hotspot user find ] do={/ip hotspot user remove \$counter}
Notes:
You can modify how often you want each of the scripts to be run depending on your bandwidth requirements however you will need to clear the manually created user accounts if you wish for the user to be able to log back in again.
Hello
I trying to use this script but is not working properly.
Trial user is blocked by script instantly – not reaching even close to downquotamb
MT 5.23 – Need some changes?
BTW. In script to clear manually created user accounts there is a mistake : \$counter} instead : $counter}
Got it working fine on a 951 running 5.23 – thanks heaps for this, the functionality is brilliant. Only tested it in the office so far and it cuts the users off nicely.
I found mistake,
Result of ” :local downquota [$downquotamb * 1000] ” is downquotamb not multiplied by 1000 !
just put :local downquota xxxx , (xxxx – mbytes in bytes and it’s working )
i tried this and it removes all users over quota, not just trial users.
the script to add them back on does nothing.
my router v5.22
Hi Murray,
My apologies, it is intended to create the trial users as local users MikroTik then clear all of these each night.
You are correct, it will clear all locally created hotspot users in it’s present form, but will not affect radius or user-manager based accounts.
Hi Omega As soon as I enabled the first scheduled script, the first time the script ran it added the limit of 50k to all users logged in, including users set up in the user manager. Obviously 50k is very little and all users were informed they were over quota.
I don’t know that much about scripting, but in my limited knowledge:
Line 10 finds all users who are logged in, not only the trial users.
Line 5 should be * 1000000 instead of * 1000
Hi there , a Nice script , but as Murray , writes it affects all users also the user manager based accounts , it would be nice if it could work on profiles ore wildcard as all trial users have a name starting like T-**
The following handles the trial users only and leaves the rest alone. It also makes the downquotamb actually represent a true MB.
:local downquotamb 100 ;
:local downquota [$downquotamb * 1024 * 1024] ;
/ip hotspot active
:foreach i in=[find] do={
:local user [get $i user];
:local mac [get $i mac-address];
:local datadown ;
:if ( $user=”T-$mac”) do={
:set datadown [ get $i bytes-out] ;
:if ($datadown>$downquota) do={
/ip hotspot
user remove [user find where name=$user] ;
user add name=$user limit-bytes-out=$downquota mac-address=$mac ;
active remove $i ;
:log info “Logged out $user – Reached download quota” ;
}}}
Next I’m gonna work on automating the clear user script so it will schedule the removal 24 hours later.
Excellent work there, looks like you’ve got it all figured 🙂
Are you trying to clear each user account 24 hours after it was created or every 24 hours?
If it was every 24 hours it would done =D
I would just use your 2nd part and schedule it to run every 24 hours.
Shouldn’t be too hard tho, I just gotta sit down and do it. Just need to get the current time and the active uptime, then do some math, then create a new schedule to delete the account. They can then create a new trial account and repeat.
I was gonna work on it next week, but now that I have it laid out here….
I may be back soon =D
Yay! THIS should do it!
I wasted a bunch of time trying to work the the date string.
I actually got that worked out where I could properly show the login date.
I even accounted for month rollovers using an array of month lengths. Then realized I didn’t need it for the removal script and it was just over kill. Still nice to have tho =D
Sorry for the repost, but had to try to see if i can get the syntax highlighting to work.
Hello
I trying to use this script but is not working
not blocket user trial
I found the mistake
this is working to me
#Download limit in 5MB
:log warning “verificando si existen usuarios Libres con exceso de descargas”
:local downquotamb “5242880”;
### Do not modify anything below this line ###
:local downquota [$downquotamb];
:local counter
:local datadown
:local username
:local macaddress
:foreach counter in=[/ip hotspot active find ] do={
:set datadown [/ip hotspot active get $counter bytes-out]
:if ($datadown>$downquota) do={
:set username [/ip hotspot active get $counter user]
:set macaddress [/ip hotspot active get $counter mac-address]
:if ($username=”T-$macaddress”) do={
/ip hotspot active remove $counter
:log info “El usuario $username – fue expulsado por exceder el limite de descarga”
}
}}
Dear thank you for this script
I just have a question
I recently activated the “trail” on hotspot and it’s working fine
there is one problem when the reboot everyone who used “trail” before will be able to use it again “note that I’m using Hotspot + User Manager
but still the same problem we reboot the router all the trail users who used it will be able to use it again ??
is there any solution for that