Add a data limit to trial hotspot users

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

Hotspot
I spy, WiFi!

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.

15 thoughts on “Add a data limit to trial hotspot users

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

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

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

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

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

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

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

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

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

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

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

    
    ##Download limit in MB
    :local downquotamb 100 ;
    ##Set "Constants"  
    :local downquota [$downquotamb * 1024 * 1024] ;
    ##loop thru each active user
    /ip hotspot active
    :foreach i in=[find] do={  
    :local user [get $i user]; 
    :local mac [get $i mac-address];
    :local datadown [ get $i bytes-out];
    :local uptime [get $i uptime] ;
    ##if user is trial    
    :if ( $user="T-$mac") do={
    :if ($datadown>$downquota) do={
    ##Remove Dynamic Trial User Acct and replace with tmp acct to disable trial access
    /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" ;
    ##Find Session Start  
    :local currTime [/system clock get time] ;
    :local startTime ($currTime-$uptime);
    ##Check if session began before midnight today
    ##it is recursive in case a session lasts several days somehow
    :while ($startTime<0) do={ :set startTime [($startTime+1d)] ; }
    ##Schedule Self-Deleting Event to remove tmp acct, allowing trial access once more
    /system scheduler 
    add name="Remove $user" start-time="$startTime" interval=1m \
    comment="Auto Generated Script - Removal of Trial Account: $user" \
    on-event="/ip hotspot user \r\
    \nremove [find where name=$user] ; \r\
    \n:log info \"Trial Account Available: $user\" ; \r\
    \n/system scheduler\r\
    \nremove \"Remove $user\" ; \r\
    \n:log info \"Schedule has been removed: $user\" ; \r\
    \n    \r\
    \n" ; 
    ##end if user over limit        
    }
    ##end if user is trial        
    }
    ##end Active User Loop
    }
    
    
  12. Sorry for the repost, but had to try to see if i can get the syntax highlighting to work.

    
    ##Download limit in MB - Editable
    :local downquotamb 100 ;
    ##Set "Constants"  
    :local downquota [$downquotamb * 1024 * 1024] ;
    ##loop thru each active user
    /ip hotspot active
    :foreach i in=[find] do={  
    :local user [get $i user]; 
    :local mac [get $i mac-address];
    :local datadown [ get $i bytes-out];
    :local uptime [get $i uptime] ;
    ##if user is trial    
    :if ( $user="T-$mac") do={
    :if ($datadown>$downquota) do={
    ##Remove Dynamic Trial User Acct and replace with tmp acct to disable trial access
    /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" ;
    ##Find Session Start  
    :local currTime [/system clock get time] ;
    :local startTime ($currTime-$uptime);
    ##Check if session began before midnight today
    ##it is recursive in case a session lasts several days somehow
    :while ($startTime<0) do={ :set startTime [($startTime+1d)] ; }
    ##Schedule Self-Deleting Event to remove tmp acct, allowing trial access once more
    /system scheduler 
    add name="Remove $user" start-time="$startTime" interval=1m \
    comment="Auto Generated Script - Removal of Trial Account: $user" \
    on-event="/ip hotspot user \r\
    \nremove [find where name=$user] ; \r\
    \n:log info \"Trial Account Available: $user\" ; \r\
    \n/system scheduler\r\
    \nremove \"Remove $user\" ; \r\
    \n:log info \"Schedule has been removed: $user\" ; \r\
    \n    \r\
    \n" ; 
    ##end if user over limit        
    }
    ##end if user is trial        
    }
    ##end Acitve User Loop
    }
    
    
  13. 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”
    }
    }}

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

Leave a Reply