Mikrotik Hotspot Data Limit Trial

Mikrotik's hotspot service is quite amazing considering what it costs. It does have some limitations though. One of these is the fact that you cannot set a data limit to the trial account, only a time limit.

Luckily there are a few workarounds for this, the original concept for this comes from www.mikrotik-routeros.com but I have corrected a couple of bugs and added some functionality.


Start by creating your FreeUser profile:
IP>HOTSPOT>USER PROFILES>ADD

Name: FreeUser

You can customize this as you like, my preference is to only allocate 512k of bandwidth to the free users



Create the following two scripts on your routerboard by going to SYSTEM>SCRIPTS>ADD

Script 1:
The first script is the one that checks if a user has reached your specified limit, you can change the limit in the second line of the script.

Name: limitdata

#Set your dowload limit in MegaBYTES!
:local downloadlimitmb "50"

### You will not need to edit anything below this line ###
:local downloadlimit  [($downloadlimitmb  * 1048576)]
:local counter
:local datadown
:local username
:local macaddress
:foreach counter in=[/ip hotspot active find where user~"^[T][-].{17}"] do={
:set datadown [/ip hotspot active get $counter bytes-out]
:if ($datadown>$downloadlimit) 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 profile=FreeUser]
/ip hotspot user add name=$username limit-bytes-out=$downloadlimit mac-address=$macaddress profile=FreeUser
/ip hotspot active remove $counter
:log info "Force logout on user: $username - Reached download quota"
}}

Script 2:
The second script resets the counters of all free users so that they can receive a new allocation.

Name: clearusers
:log info "Checking Users"
:local counter

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

You then need to add two scheduled tasks under SYSTEM>SCHEDULER:

The first task will determine how often a specific mac address' usage is reset. I run it at midnight every day, which means that in my example people can use 50MB per day for free.
add disabled=no interval=1d name=ClearUsers on-event=clearusers policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=jan/01/2002 start-time=00:00:01

The second task will determine how often the system will check if a user has reached the quota. I run it every ten seconds, but on a busy routerboard you may have to set this to a higher value:
add disabled=no interval=10s name=DataLimit on-event=limitdata policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=oct/07/2013 start-time=00:00:00

You should now be able to have users use the predefined trial without needing a login. Just remeber to enable to trial option for your hotspot.

Comments

Popular posts from this blog

DMASOFTLAB RADIUS MANAGER BILLING SYSTEM v 4.1 Finally Released

How To Configure Nano Station M2 As Access point