MikroTik Hotspot User Management Script
MikroTik Hotspot User Management Script
A DHCP lease entry is created when it is verified that the user is connected. The script commands `/ip dhcp-server lease add` with associated variables for IP and MAC address when it detects the user's activity via MAC address and IP being actively retrieved, ensuring resource allocation is only for active users .
The script checks if the user's comment field is empty and assigns the username as the comment if it's missing. This is done with the conditional statement: `:if ( [ /ip hotspot user get $user comment ] = "" ) do={ [ /ip hotspot user set $user comment=$user] }`. This ensures that each DHCP lease can be tracked back to the corresponding user through their username .
The script logs user activities by storing messages that include the username, MAC address, and IP address. This is performed using the `:log info message=` command. Logging these details is critical for monitoring user behavior, diagnosing network issues, and maintaining security by providing an audit trail of network activity .
Upon logout, the script removes any existing configurations associated with the user such as IP binding, DHCP server lease, simple queue entries, scheduler events, and script entries. This ensures that any allocated resources or limits are released, using commands like `/ip hotspot ip-binding remove`, `ip dhcp-server lease remove`, and `/queue simple remove` .
The script maintains network performance limitations by using simple queues to restrict bandwidth. It adds an entry for each user with a max limit of 2M/2M, implying that each user can use up to 2 Mbps for both upload and download, as indicated in `/queue simple add name="$a" target="$b" max-limit=2M/2M` .
The scheduler is used to periodically execute the user-specific script to ensure any configurations related to a user are reset or updated at a defined interval. Its timing is configured through the `system scheduler add` command, which sets a recurrent start-time and specifies policies to control execution rights (read, write), and the event callback function (`on-event=$user`).
Removing user-related scripts and records after logout stops unnecessary consumption of resources like IP addresses, queue slots, and processing time on the network. By clearing these records, it minimizes wasted resources and ensures that new users can be readily accommodated, optimizing the network's resource allocation .
The 'bypassed' IP binding type allows a user to bypass the Hotspot login process after their initial authentication. This is important for improving network efficiency and user experience, as it means the script configures the device to automatically revert to a state where they have direct access to network resources without repeated authentication .
The system script adds a user-specific script that automates the removal of user-specific configurations such as IP bindings, DHCP leases, and queue limits. It allows automatic management of resources by deleting these configurations after a fixed interval or upon certain conditions being met, thereby maintaining efficient use of network resources and ensuring a clean state when users log out .
The script strategically uses local variables, such as `:local a $user`, to store and reference user-specific information such as usernames, MAC addresses, and IP addresses. This use of variables streamlines the script's operations by enabling straightforward updates and retrievals of user-specific data across different configurations in a cohesive manner .