DHCP-Client script: update status during script

Hello and please excuse this noob question…

I have a simple script inside the DHCP-Client with a delay of 10s in between. I would like to have the DHCP-Client status ($bound) to be updated or lets say checked again after this delay. At the moment the whole script is worked thru with the status when the script was triggered.
So is there something like “get $bound” that I can put in between? What’s the syntax for?

Also if I run from the console:
:if ($bound!=1) do={…
This works, as I have only one DHCP-Client enabled. How would I define one within this command. Maybe by telling the interface or the comment?

Thanks a lot!

Local variable declaration:

:local bound

Local variable declaration and initialisation with boolean value for eg.:

:local bound true

Update variable value:

:set bound false

See more at https://wiki.mikrotik.com/wiki/Manual:Scripting#Variables

Also, you don’t need to check value for boolean variable in condition, :if ($bound) do={… or :if (!$bound) do={… will work.

Well, I think in this case I just define my own variable called bound and tell it to be true or false, which I think is 1 or 0.
From what I understand reading the DHCP-Client help, there is a $bound per default which I successfully use within my script – without declaring it by my own.
And I don’t want to tell this variable what to be but it should update its value depending on the current dhcp client status at a given time/line somewhere in the script.

Are you referring to DHCP lease script? If yes, then there is leaseBound variable set (1|0) when script triggers, see https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server#General:

Script that will be executed after lease is assigned or de-assigned. Internal “global” variables that can be used in the script:
leaseBound - set to “1” if bound, otherwise set to “0”
leaseServerName - dhcp server name
leaseActMAC - active mac address
leaseActIP - active IP address
lease-hostname - client hostname
lease-options - array of received options

But these variables are meant to be read-only, having delays in that script and checking that same variable or updating it… not sure what you are trying to accomplish…

No I refer to this – dchp-CLIENT
https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Client#Properties

Let me explain:
If I hit the release button in the dhcp-client window, status changes from bound to searching just for a second and back to bound. The script is triggered two times within a second in this case. So first the script works thru all the $bound!=1 stuff and a second later thru the $bound=1 stuff. In my case enabling IP address etc and disabling a second later.
If I add a delay of 5s at the beginning of the script I would assume that $bound, as it changed back to 1 within a second, is handled as 1 but it is handled as 0 as this was the value as the script was triggered to start. So I would need a function to re-read $bound right after the 5s delay in the script.
The script may still be triggered two times but running thru both times with the same value of $bound.
Well but this is not a big problem, more an ecstatic issue. I just wanted to avoid immediate changes if the client has some issue just for a second while renewing or whatever.

But I have one more problem right now:
:if ($bound!=1) do={…
is not working from terminal or inside startup script. Maybe because $bound from dhcp-client is not a global variable?

What I want is to have the dhcp-client active for about 30s after boot, and disable it if it does not receive an IP which means there is no dhcp server on the network. And then it should enable a static address and its own dhcp-server.

Thanks for your help!

$lease-address seems also to be unusable as within the dhcp-client script. At the moment when the status changes to bound the script is triggered but the ip has not yet been received. So the variable is empty during the script. So the script has to be started with a delay and variables have to be updated then to go thru the script.

However, I put most of the stuff out of the dhcp-client script into a separate script and found the way to access these values:
:if ([/ip dhcp-client get client1 status] != “bound”) do={…
So I can access the same values of the variables inside the client-dhcp script which seem not to be global.

Post your script - you shouldn’t need a 2nd script…

But you may need quotes or parenthesis… e.g. ($“lease-address”) should have the obtained IP.

The variables in DHCP client are BUT they are NOT globals and “local” to the DHCP client script only:

bound - 1 - lease is added/changed; 0 - lease is removed
server-address - server address
lease-address - lease address provided by server
interface - name of interface on which client is configured
gateway-address - gateway address provided by server
vendor-specific - stores value of option 43 received from DHCP server
lease-options - array of received options

Check if Script is already running:
http://forum.mikrotik.com/t/prevent-the-script-from-running-if-it-is-already-running/167618/3

Terminate other istances of the same Script. No matter what is the script name, just make unique the UniqueScriptID

:local UniqueScriptID "QnJhdm8h"
/system script
:local ThisScriptName [get ([find where source~"$UniqueScriptID"]->0) name]
:local searchJobs     [job find where script=$ThisScriptName]
:if ([:len $searchJobs] > 1) do={ :for idx from=0 to=[:len $searchJobs] step=1 do={ job remove ($searchJobs->$idx) } }

# simulating script running for 60 seconds
:delay 60s

This is my startup script, there is one issue with it:
the while ([/ip dhcp-client get client1 status] != “bound”) needs to be leaved after count = 30 but as status does not change script stays in the loop and does not go on. Is there some kind of GOTO or another command to leave/exit the loop?

/ip dhcp-server disable server1
/ip address disable "single-AP"
/ip dhcp-client enable client1

# wait for wlan-interfaces
:local count 0;
  :while ([/interface wireless print count-only] < 2) do={
    :if ($count = 60) do={
      :log warning "Startup-Script: Unable to find wireless interfaces";
      /quit
    }
    :set count ($count +1);
    :delay 1s;
  };

/interface disable wlan1
/interface disable wlan2

# wait for dhcp-client bound
:set count 0;
  :while ([/ip dhcp-client get client1 status] != "bound" && count < 31) do={
    :if ($count = 30) do={
      /ip dhcp-client disable client1;
      :log info "bound=false";
      /ip address enable "single-AP";
      /ip dhcp-server enable server1;
    }
    :set count ($count +1);
    :delay 1s;
  };

:if ([/ip dhcp-client get client1 status] = "bound") do={:log info "bound=true"}

/interface enable wlan1
/interface enable wlan2

/quit

This is the dhcp-client script:

:if ($bound=1) do={:log info ("bound=" .$bound); /ip dhcp-server disable server1; /ip address disable "single-AP"}
:if ($bound=0) do={:log info ("bound=" .$bound); /ip address enable "single-AP"; /ip dhcp-server enable server1}

I just found the solution for the first (startup) script by adding $count as a second condition:

([/ip dhcp-client get client1 status] != "bound" && count < 31)

I changed this line in my original post above, so the script is ready to use if anyone needs it.
The second script (dhcp-client script) is optional. If used the AP would change back to fixed IP with dhcp-server if dhcp-client bound is lost and vice versa. In this case the dhcp-client must not be disabled by the first script if there is no bound. If not used, the AP has to be restarted to check again for dhcp-server on the net.

Although my initial question about a delay in dhcp-client script and/or a way to update the variables with their current values at some point in that script is still open.

For me this is a perfect solution and it seems to work great. Have to test it in use case now.

Thanks for helping out!

Okay, so the actually problem is you want have DHCP Client listen for an address & if it doesn’t get one, after some timeout, then enabled the interface for DHCP Server. That is a bit tricky…

The $bound, $gateway-address, etc variables are local and provide per call to the script. One important thing is the $bound=0 will only get called if got an address and then later cannot renew it, e.g. if it never gets an address you’d never get $bound=0 state.

So unsure of your question about the delay part here…

Okay, so the actually problem is you want have DHCP Client listen for an address & if it doesn't get one, after some timeout, then enabled the interface for DHCP Server. That is a bit tricky....

Well, that's exactly what my startup-script does and it works.
I have decided to not use the dhcp-client script so the AP will define it's state after reboot only and stay in this config until manually rebooted or turned off/on.

The dhcp-client script would offer the possibility to change the APs config later if at some time for any reason a dhcp-server is present and therefore make itself a "stupid" client again.
You are right about the $bound=0 problem.

However, as I decided to go the way without the dhcp-client script, there is no need to waste your time on my question and it's OK for me if no one responds anymore.

But if some are interested, I just explain my question again:
As soon the dhcp-client state changes, the script gets triggered. But it can last one second more until an IP-Address is received. So $lease-address is still empty when the script gets started. I tried to start the script with a delay of 5s, so the IP was received. But for the script it is still empty.
For me it seems like that all variables are read out and declared once the script gets started. It would be better if these variables will be read out (or updated) every time in the script when they are used. Or to have the possibility to update it manually.

So I think it's something like this:
script is triggered because dhcp-client status changed.
:local $bound = 1
:local $lease-address = 0
:local ...
...all status variables are declared automatically by the system
:delay 5s ... I set a delay to wait for the lease-address to be received

would like to have something like :update $lease-address to re-read the value of the lease-address

:log info ("IP=" .$lease-address) ... lease-address is still empty

Kind regards!

I was a few days off and saw a response in between but now it is not there anymore. How can this be?

However, I had some time to investigate this one more and found a solution for the dhcp-client script:

As the $lease-address keeps empty for me every time the dhcp-client script runs I just used…

:local IP "$[/ip dhcp-client get client1 address]";

:if ([:len $IP] != 0) do={:log info "IP=$IP"};
:if ([:len $IP] = 0) do={:log info "no IP"};

:if ([$IP] = "192.168.88.123/24") do={:log info "IP=maybe good"};
:if ([$IP] != "192.168.88.123/24") do={:log info "IP=maybe bad"};

and at last, thanks to everyone for helping!

I can understand that a novice doesn’t know scripting, but where have you seen it done this way?
:local IP “$[/ip …]”;

:local IP [/ip dhcp-client get client1 address]

:if ([:len $IP] != 0) do={
:log info “IP=$IP”
} else={
:log info “no IP”
}

:if ([$IP] = “192.168.88.123/24”) do={
:log info “IP=maybe good”
} else={
:log info “IP=maybe bad”
}

I’m sorry! My brain is so bad in understanding syntax. I struggle a lot with such things.
I did not read this anywhere, I was just testing.

Thanks for this very important and polite response!