Page 1 of 1

hotspot - make lease static after login

Posted: Sat Apr 06, 2019 12:13 am
by aleksanderm
Hello,

I'm trying to setup an "advenced" hotspot on the RouterOS. Basic configuration covered by official documentation works
like a charm but the goal is to create the hotspot for a dormitory with additional rules:

1. After first login, the Mac Address should be bound to the user

2. The static lease should be created on the DHCP to bind Mac Address to the IP - we want the user to use "static" address assigned from dhcp and
we don't want this address to be changed during the accommodation.

I came out with hotspot user profile on login script:
:local u [/ip hotspot user find name=$username ];
:if ([:len $u] > 0) do {
    :if ([:len [/ip hotspot user get $u "mac-address"]] < 1) do {
        # bind mac address to the user
        /ip hotspot user set $u mac-address=$"mac-address";
        # convert the dynamic lease into the static lease
        :local lea [/ip dhcp-server lease find mac-address=$"mac-address"];
        log info message=$lea;
        /ip dhcp-server lease make-static $lea;
    }
}
the first part works (binding mac to user) but unfortunatelly, for some reason, after each login,
all the leases are coverted to the static leases. Following line:
/ip dhcp-server lease find mac-address=$"mac-address"
returns array containing ALL leases and should return only one lease which corresponds to the mac address.
I can see it in the log: "*2;*3;*4"

funny thing is, when I try to execute command like:
:put [/ip dhcp-server lease find mac-address=08:00:27:8B:C4:CD]
outside of the script, it displays only ONE element (as expected).

What am'i missing? Is it bug or my mistake?

Best regards,
Alek.

Re: hotspot - make lease static after login  [SOLVED]

Posted: Tue Apr 16, 2019 12:51 pm
by eworm
Well, an odd behavior in RouterOS... Mikrotik support says "that's how scripting works in RouterOS".

You can not use same variable name and option name. I switched to use variable names in CamelCase. Just replace $"mac-address" with $MacAddress.

Read my detailed analysis and solution here:
global: variable names are CamelCase

Re: hotspot - make lease static after login

Posted: Mon Jun 17, 2019 11:00 pm
by aleksanderm
Hello, support gave me similar answer. As you noticed very strange language "feature".

Best regards,
Aleksander