This repository provides a straightforward method to automatically configure bandwidth allocation for DHCP clients based on their MAC addresses using MikroTik scripts. This setup ensures efficient bandwidth management for your network.
- Dynamic Bandwidth Allocation: Automatically assigns bandwidth limits based on the MAC address of DHCP clients.
- Queue Management: Utilizes PCQ (Per Connection Queue) types for optimized upload and download rates.
- Scalable Solution: Configures queues dynamically, ensuring seamless management for growing networks.
- MikroTik RouterOS with DHCP Server enabled.
- Winbox (or terminal access to the router).
- Basic familiarity with MikroTik scripting and queue management.
First, configure PCQ (Per Connection Queue) types for both upload and download rates. These queues ensure that bandwidth is allocated proportionally among clients. Follow these steps:
- Open the MikroTik terminal in Winbox.
- Run the following commands to create PCQ queues:
/queue type add kind=pcq name=pcq_download pcq-rate=500M pcq-classifier=dst-address
/queue type add kind=pcq name=pcq_upload pcq-rate=500M pcq-classifier=src-address
These queues specify the bandwidth limits (500 Mbps in this case) for download and upload. You can adjust the pcq-rate
value as per your network requirements.
Use the following script to dynamically create or remove queues based on the DHCP lease status of a client.
:local queueName "Client- $leaseActMAC";
:if ($leaseBound = "1") do={
/queue simple add name=$queueName target=($leaseActIP . "/32") queue=pcq_upload/pcq_download comment=[/ip dhcp-server lease get [find where active-mac-address=$leaseActMAC && active-address=$leaseActIP] host-name];
} else={
/queue simple remove $queueName
}
- Lease Bound (Connected): When a DHCP lease is bound to a client, the script dynamically adds a simple queue using the client’s MAC and IP address.
- Lease Unbound (Disconnected): If a client disconnects, the corresponding queue is removed automatically.
- Go to IP > DHCP Server > Leases in Winbox.
- Open the Script tab.
- Paste the script provided above.
- Save the changes.
For a client with:
- MAC Address:
00:1A:2B:3C:4D:5E
- IP Address:
192.168.1.10
The script dynamically creates a queue named Client-00:1A:2B:3C:4D:5E
and assigns the specified PCQ limits for uploads and downloads.
- Automated Management: Eliminates the need for manual queue creation.
- Efficiency: Dynamically removes queues for inactive clients, reducing unnecessary load.
- Scalability: Supports networks of any size with minimal configuration adjustments.
- Queue Names: Modify the
queueName
variable format as needed. - Bandwidth Limits: Adjust
pcq-rate
values in the PCQ setup commands for custom limits. - Script Enhancements: Add logging or notification features for better monitoring.
- Queues Not Created: Ensure that the DHCP lease script is enabled and correctly configured.
- Incorrect Bandwidth Allocation: Verify PCQ classifier settings and rates.
- Script Errors: Test the script in the MikroTik terminal to debug issues.
Contributions are welcome! If you have suggestions for improvements or additional features, feel free to submit a pull request.
This project is licensed under the MIT License. See the LICENSE
file for more details.
Enjoy automated bandwidth management with MikroTik!