I use a regualar expression to match. That is the ~ sign instead of a = .
The searched interface name has to be also unique or there will no match. If you have two PPPoE (pppoe-in pppoe-out) then matching on pppoe does not cut. You have to match on the difference and the shortest one is “in” or “out” . pppoe alone will not match and so no result.
:local xx "pppoe"; :put [typeof $xx]; :put [/ip/address/get [find interface~$xx]]
I use :local and it display the type of the variable. That is in this case a string (str), so no need to put the variable $xx in quotes.
Returned type is an array of a single element, get command does not accept an array, either use pick command to pick the 0 element of an array or if you are sure that there is only one element then convert array to string with :tostr command
All tested on v6, but I do not know any reason for wat must do not work on v7, let me know, please.
:global iface "pppoe-out-fb"
:put ([/interface pppoe-client monitor $iface once as-value]->"local-address")
# do error if interface not exist
# do not contain /xx
:global iface "pppoe-out-fb"
:put [/ip address get [:tostr [find where interface=$iface]] address]
# do error if interface not exist or if interface have multiple IPs
# contain /xx and must be removed if needed
:global iface "pppoe-out-fb"
:put [/ip address get ([find where interface=$iface]->0) address]
# do error if interface not exist or if interface have no IP
# contain /xx and must be removed if needed
# on ppp profile used, on on-up script:
:global fbIP $"local-address"
# do not contain /xx
A follow-up as I’ve ran into similar issue, but the cause was not apparent from this thread.
:local interface "some-interface"
:put [/ip dhcp-client get [find interface=$interface] primary-dns]
Results in “invalid internal item number”.
:put [/ip dhcp-client get [find interface="some-interface"] primary-dns]
Works well.
:local dhcpinterface "some-interface"
:put [/ip dhcp-client get [find interface=$dhcpinterface] primary-dns]
Works well.
Tested on ROS 7.9.
The cause of error in the particular case is name clash in between parameter name “interface” and variable name “interface”, use of a variable with different name solves the issue.
You broke a simple law of almost all programming languages: don’t use reserved words as a variable name…
If the context has “interface” as the element name, obviously you must NOT use the same name.
That’s why I wrote “iface”.
You broke a simple law of almost all programming languages: don’t use reserved words as a variable name…
Sure. The problem is DHCP client supplies interface name to the script as variable named “interface” (unlike DHCP server that uses “bindingVariableName” variables). Had to use a proxy variable instead.
I know it’s in the docs (now, when I searched them for reserved variable names), but, honestly, the clash was NOT obvious - from implementation POV I’ve expected variables to be substituted first, in the shell or script interpreter, before the commands are executed (thus no clash is possible - the command sees no variable names), but the commands just inherit script environment and substute variables internally (thus causing clash and this, rather cryptic, error message).
An interesting feature of this internal variable substitution is ability to use values of other properties for inline filtering:
/ip firewall address-list print where address=$comment
Will print all IPv4 firewall address list entries in which the address is the same as the comment.
This, however, seems to work for find and where only.
While it looks more in line with “print where” this way, both docs page on scripting and /export command use “[find property=value]” without “where”. Terminal autocomplete suggests that “where” is optional for “find” too.
EDIT: I don’t understand anything, but there was a host in the DHCP Lease list that made it give that error, after disabling it it is working again. I am sorry for this.