Non-customers trying to login, also Logged Out and expired Limit Uptime occupy DHCP Leases unnecessarily.
- Lease Time of DHCP Server is set to 30 days for some customers, we also have lesser durations, hence I can’t set lesser than 30 days as they keep getting logged out.
Script objective:
If ‘dhcp-server lease ip’ not match any ‘hotspot active ip’, remove such lease from dhcp-server:
Kindly help achieve the script, I have tried this below but it doesn’t work.
foreach i in=[/ip dhcp-server lease find] do={
:if ([:len [/ip dhcp-server lease get $i host-name]] != [/ip hotspot active get $i address]) do={
/ip dhcp-server lease remove $i;
}
}
Thank you 
Add comment =“reserved” to corresponding DHCP Leases on login (those that don’t login will have empty comments)
IP > Hotspot > User Profiles > Name > Scripts > On Login (long term user profiles only):
:local userInfo $user;
:global reserveMac [/ip hotspot active get [find user=$userInfo] mac-address];
/ip dhcp-server lease set comment=“reserved” [find mac-address=“$reserveMac”];
Schedule remove DHCP Leases with empty comments at 7 AM every morning
System > Scheduler > New and choose start time (e.g. 7 AM) and other details, adding On Event:
/ip dhcp-server lease remove [find comment!=“reserved”]
Resulting of daily removal of all leases except long term users, in our case 30 days.
#Additionally, you can remove global variables like this - not sure why I would remove the variable after its been used, but its possible to tidy things up unnecessarily:
/system script environment remove reserveMac
#to see all Environment Variables:
/system script environment print
#to Remove Environment Variables:
/system script environment remove 1 or any other variable
I hope you find this useful.
A lower dhcp lease time does not cause a log out to the Hotspot client…
You are right, Leases are automatically removed on expiry.
Only logged-in and status ‘Waiting’ (if Lease is Static) remain in list.
Lease Expiry is now 6 hours, /IP Pool subnet /24 with 254 IP addresses, should be enough.
How to set pool size:
/IP Addresses (add if not exist or modify current range) Address 192.168.4.1/22, Network 192.168.4.0, Interface ‘bridge-hotspot’ (bridge created earlier for hotspot)
/IP Pool (add if not exist or modify current range) 192.168.4.1-192.168.6.254
* Note in ‘/IP Pool’ there exist another pool within the main router IP range, I am unsure if that could be removed so keep it unless you know if its needed or not (e.g. default-dhcp 192.168.88.10-192.168.88.254)
/IP DHCP-Server Networks (add if not exist or modify current address) Address = 192.168.4.0/22, Gateway = 192.168.4.1
Thank you.