Community discussions

MikroTik App
 
User avatar
Edoras
newbie
Topic Author
Posts: 35
Joined: Wed Apr 05, 2006 12:03 am
Location: Prague, Czech rep.

get IP ADDRESSES on interface

Tue Jun 20, 2017 1:36 pm

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
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7169
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: get IP ADDRESSES on interface

Tue Jun 20, 2017 1:39 pm

because it will return an array of all address IDs on that interface.
Run loop through the array to get individual ids.
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Wed Mar 13, 2024 10:20 am

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?
 
User avatar
diamuxin
Member
Member
Posts: 330
Joined: Thu Sep 09, 2021 5:46 pm

Re: get IP ADDRESSES on interface

Wed Mar 13, 2024 2:15 pm

{
/ip cloud
:local ipv4 [get public-address]
:local ipv6 [get public-address-ipv6]
:put $ipv4
:put $ipv6
}
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Wed Mar 13, 2024 4:18 pm

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.
 
trkk
just joined
Posts: 17
Joined: Tue Mar 08, 2022 1:36 pm
Location: +09:00

Re: get IP ADDRESSES on interface

Wed Mar 13, 2024 4:54 pm

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
}
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Wed Mar 13, 2024 7:55 pm

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?
 
trkk
just joined
Posts: 17
Joined: Tue Mar 08, 2022 1:36 pm
Location: +09:00

Re: get IP ADDRESSES on interface

Thu Mar 14, 2024 7:32 am

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 ;-).
 
trkk
just joined
Posts: 17
Joined: Tue Mar 08, 2022 1:36 pm
Location: +09:00

Re: get IP ADDRESSES on interface

Thu Mar 14, 2024 7:50 am

Correction.
.., we can see usable query parameters by
"dynamic" and "invalid" are not shown in "F1" help message, but usable.
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Thu Mar 14, 2024 11:50 am

Sorry, I confused you and OP.
Okay, all right.
Unfortunately there is no flag that indicates the IPv4 address is global or not
And for IPv6 is there any way?
 
trkk
just joined
Posts: 17
Joined: Tue Mar 08, 2022 1:36 pm
Location: +09:00

Re: get IP ADDRESSES on interface

Thu Mar 14, 2024 2:38 pm

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
}
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Thu Mar 14, 2024 7:40 pm

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get IP ADDRESSES on interface

Fri Mar 15, 2024 6:01 pm

examples code

/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.
 
Radek01
just joined
Posts: 19
Joined: Wed Mar 01, 2023 11:20 am

Re: get IP ADDRESSES on interface

Sat Mar 16, 2024 11:32 am

rextended: Thank you very much. Good to know.

Who is online

Users browsing this forum: No registered users and 17 guests