get IP ADDRESSES on interface

I want to use IP of interface in my script. I get IP address by command

:global addr [/ip address get [find interface="bridge1"] address]

If on the interface is just one IP, it works. But when on the interface are two or more IPs, it displays error message “invalid internal item number” . Any ideas?

Thanks, Jiri

because it will return an array of all address IDs on that interface.
Run loop through the array to get individual ids.

Is there any option to filter it by their flags? For example, in IPv6, an interface has several addresses with different flags, and if I only need to get the global address, how can I do that?

{
/ip cloud
:local ipv4 [get public-address]
:local ipv6 [get public-address-ipv6]
:put $ipv4
:put $ipv6
}

diamuxin: Thanks for reply, but I was referring to getting an IP address in general, not just a public. Specifically, the cloud is good choice for somebody but I am not using.

Doesn’t this help you?

{
    :local addrs [:toarray ""]
    /ip/address
    :foreach id in=[find interface="bridge1"] do={
        # do something, like
        :set addrs {$addrs , [get $id address]}
    }
    :put $addrs
}

trkk: This could be a solution for the original interviewer, but it won’t help me. I am looking for a way to filter and get IP addresses according to their flags. I asked my question in this thread because it applies to the same topic, but I am not the author of the original question.

By the way, the :put command should be inside of the for loop, right?

Sorry, I confused you and OP.
Unfortunately there is no flag that indicates the IPv4 address is global or not, so we have to do like this.

> :put [/ip/address/find where !(address in 192.168.0.0/16) and !(address in 172.16.0.0/12) and !(address in 10.0.0.0/8)]

You may know, we can see usable query parameters by

> /ip/address/find where <press F1 here>

And about

By the way, the :put command should be inside of the for loop, right?

the answer is No.
That code stores addresses into an array then puts array’s data at outside of the loop. Maybe I had to write “addresses” rather than shorten “addrs” (= addr + ‘s’ of plural form, sorry :wink:.

Correction.

.., we can see usable query parameters by

“dynamic” and “invalid” are not shown in “F1” help message, but usable.

Okay, all right.

Unfortunately there is no flag that indicates the IPv4 address is global or not

And for IPv6 is there any way?

Ah, I see. I read your qustion wrongly, like
“/ip/address doesn’t have parameters (flags) unlinke IPv6 (/ipv6/address), so I can’t pick up global IPv4 addresses.”

So, if you want to pick up global IPv6 addresses, simply do “/ipv6/address/find where global”.

{
    :local addresses [:toarray ""]
    /ipv6/address
    :foreach id in=[find interface="bridge1" global] do={
        # do something, like
        :set addresses {$addresses , [get $id address]}
    }
    :put $addresses
}

OK, thanks for the kick. Now I found out that besides global, invalid and dynamic, link-local is also usable. Static can’t be used as the parameter, I don’t even know why. Are these existing and usable parameters listed somewhere in the Mikrotik documentation?

/ipv6 address print where interface=WAN and global

:local wanipv6global [/ipv6 address get [find where interface=WAN and global] address]
Obviously some problems can occour if the interface have more than one address…

Trying to not forget one, one IPv6 can have one or more flags:
disabled
dynamic
global
invalid
link-local

on v7:
preferred
valid

some not exist but are easy doable:
enabled is: !disabled
static is: !dynamic

on v6: valid is: !invalid
and on v7 “not preferred” is: !preferred

Obviously some are esclusive, for example global can’t be link-local, etc.

rextended: Thank you very much. Good to know.