Community discussions

MikroTik App
 
lordwhiskey
newbie
Topic Author
Posts: 27
Joined: Wed Jun 26, 2013 8:33 am

Find function with IP address condition

Tue Dec 21, 2021 4:20 pm

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
Last edited by lordwhiskey on Wed Dec 22, 2021 3:43 pm, edited 1 time in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Find function with IP address condition

Tue Dec 21, 2021 6:51 pm

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
}
}
Last edited by Jotne on Tue Dec 21, 2021 11:01 pm, edited 2 times in total.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Find function with IP address condition

Tue Dec 21, 2021 9:44 pm

You can not use address=$address with RouterOS... See a description here:
https://git.eworm.de/cgit/routeros-scri ... 8bbde9651a

Have not looked at all of your script, but perhaps that already fixes it.
 
lordwhiskey
newbie
Topic Author
Posts: 27
Joined: Wed Jun 26, 2013 8:33 am

Re: Find function with IP address condition  [SOLVED]

Wed Dec 22, 2021 3:43 pm

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!
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: [SOLVED] - Find function with IP address condition

Wed Dec 22, 2021 8:25 pm

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

Who is online

Users browsing this forum: FurfangosFrigyes and 27 guests