The (unofficial) Mikrotik site

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

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.

Advertisement
Exit mobile version