Help in Script

Hi all,

I’m new in Scripting and i’m trying to do some scripts to help me ease the maintenance work.

I have ran the following line in terminal to change all the dhcp-server leases from always-broadcast=yes to alwaybroadcast=no:

/ip dhcp-server lease>:foreach i in=[/ip dhcp-server lease find always-broadcast=yes] do={set $i always-broadcast=no}

That line worked like a charm.

Now, i’m trying to “copy” in each lease the same “active-client-id=” that is auto generated when a device get an IP, to the “client-id=” of that lease. I’ve done some tests and I can’t get it to work, this is what i think the script is, but now i know that it’s wrong because it’s like nothing happened:

/ip dhcp-server lease>:foreach i in=[find active-client-id] do={set $i client-id=$i}

Maybe someone can point me out to the right direcction or at least tell me what the sentence should be.

Any help would be appreciated.

Thanks in advance!

I think this should be more like:

foreach i in=[/ip dhcp-server lease find where static] do={:local id [get $i active-client-id] ; set $i client-id=$id }

Thanks ZeroByte:
I had to adjust it a bit for it to work, i dont know why, but that exact syntax did nothing, i’ve just deleted “where static” and it worked as expected:

:foreach i in=[/ip dhcp-server lease find] do={:local id [get $i active-client-id ] ; set $i client-id=$id }

I really aprreciate your input! it helped me a lot!

Thanks!!

No problem!
And the “where static” should be “where ! dynamic” (if you only want to set this on static leases)

The key thing you missed in your command was that you were trying to set the client id to be $i, but $i is a reference to the index of the lease in the leases table.
The fix was to iterate through $i with the foreach loop, and to do two steps: capture the active-client-id of lease($i) in a second variable ($id) using the get command, and then using both the index and the value as variables in the set command.

Ohh.. of course.. I missed that part.. the thing is, like i said, i’m new in scripting and even at writing codes, althought I have several year of experience with RouterOS, now i’m trying to expand my knowledge about it and get more out of my RB’s.

Getting back to topic, I don’t know why, but when I ran the script code you gave me, it didn’t do anything, even when all the leases are static, thats why I started playing with it a bit and deleted the “where static” to see if something happen and doing that i saw it worked but now i realized that what it deleted the all the “client-id” of the leases already created in the table but were in “wating” status. Maybe using another condition in the “find” part that tell’s it to only look for “bound” leases, o maybe seeing it from another point of view, that condtion can be that the “client-id” is empty.

Thanks again, all this process is helping me to understand scripts.

After trying and reading, i was able to get where i wanted with this script:

:foreach i in=[/ip dhcp-server lease find where ! client-id] do={:local id [get $i active-client-id ] ; set $i client-id=$id }

As I understand it, that line will look for leases with no “client-id” and save the “active-client-id” it into the “:local id” variable and then set it on the “client-id”.

Thanks @ZeroByte for your help, you pointed me to the right direction!

After some reflection, I think you might even be able to boil the do={ } portion down a bit further:
do={set $i client-id=[get $i active-client-id]}
I’m glad to see you got the results you wanted. Happy scripting!

Try this and then check your Logs !!!

Script to Set client-id

from BrasDeutscher, PA, Brazil

Testet on RouterOS v6.37rc27

{

Set your Server Name here

:global sv “your-server-name”

!!-- Do not change any below --!!

:global action
:global id
:global mac
:foreach d in=[/ip dhcp-server lease find where server=“$sv” ! dynamic ] do={
:if ([/ip dhcp-server lease get $d active-client-id ] != “” ) do={
:set id [/ip dhcp-server lease get $d active-client-id ];
:set mac [/ip dhcp-server lease get $d active-mac-address ];
:if ([ip dhcp-server lease find where client-id=$id ] = “” ) do={
:set action [/ip dhcp-server lease set [find where active-mac-address=$mac ] client-id=$id ];
:log warning (“Setting Client ID to $id”);
}}}}