Find function with IP address condition

Hello everyone!

I’m trying to run a very simple script, but apparently I’m doing it wrongly. The steps the script should run are as follows:

  • Read a list of IP addresses from an address-list
  • For each of the IP address, check if it is a lease of the DHCP server
  • If yes, extract the comment that the specific DHCP lease entry has
  • Apply the comment to the address list entry. If not, jump to the next address

The script I’m trying to run, is the following

:foreach key,value in [/ip firewall address-list find where list=no_internet_access] do={
:local address [/ip firewall address-list get $value address];
:local comment [/ip dhcp-server lease get [/ip dhcp-server lease find address=$address] comment];
/ip firewall address-list set [/ip firewall address-list find where address=$address] comment=$comment
 }

By running the above script, I get the following message

invalid internal item number

The issue seems to come from the following line

:local comment [/ip dhcp-server lease get [/ip dhcp-server lease find address=$address] comment];

and most importantly from

[/ip dhcp-server lease find address=$address]

Indeed, the above function returns a set of indicators/ids instead of a single one. The interesting fact is that if I replace $address with an IP address (e.g., 10.0.0.1) the script works perfectly.

Note: I made a test by putting $address as a global variable and check its content; the content is correct, as it contains only one IP address.

Does anyone have a suggestione?

Thanks

I have not time or possibility to test all, but for me the script does not work at all without = after in.
No need to add ; after all lines, only between multiple commands on same line.
You can save some by go to correct folder before running commands so some rewriting

Edit as eworm writes, it may be that you have used reserved variable. Edited to not make conflicy.

/ip firewall address-list
:foreach key,value in=[find where list=no_internet_access] do={
	:local Address [get $value address]
	:local comment [/ip dhcp-server lease get [/ip dhcp-server lease find address=$Address] comment]
	set [find where address=$Address] comment=$comment
}

To test it out in terminal wrap code in {}

{
/ip firewall address-list
:foreach key,value in=[find where list=no_internet_access] do={
	:local Address [get $value address]
	:local comment [/ip dhcp-server lease get [/ip dhcp-server lease find address=$Address] comment]
	set [find where Address=$address] comment=$comment
}
}

You can not use address=$address with RouterOS… See a description here:
https://git.eworm.de/cgit/routeros-scripts/commit/?id=870f00bb36f5af3088344371764da48bbde9651a

Have not looked at all of your script, but perhaps that already fixes it.

All,

many thanks for your replies.

As mentioned by eowrm, the issue lies in the usage of a variable name that corresponds to a field name.

I was able to fix this.

Thanks again!

And also in missing = after in.
None of my routers works without the =