Community discussions

MikroTik App
 
KennethL
just joined
Topic Author
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

Find the Address-List to which an IP is assigned

Wed Oct 26, 2011 10:42 pm

Hi All,

I have searched the web, but was unable to find a solution to the following:

I have three address-lists configured, each driving workstations through three different WAN ports.

I just need a simple quick way (without using the GUI, so through the terminal) to find out to which list any particular IP belongs.. In other words, somthing like:

/ip firewall address-list get list where address=192.168.x.x

and as a return to the above, just spit out: list1, list2 or list3.

hope this makes sense, but let me know if you have any suggestions.
Thanks,
Kenneth
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Find the Address-List to which an IP is assigned

Wed Oct 26, 2011 11:44 pm

Although it's normally not a good idea to spoon feed such scripts, I've been wondering on what kind of sample scripts to work on that I might practice scripting with... so thank you for providing me with this opportunity, although I also kind'a feel gulty of taking it away from you :D .

Here's a script that does just what you said:
:local ip 192.168.x.x

:local result ""
:local delimiter ", "
:local fullList [/ip firewall address-list print as-value where address=$ip]
:foreach entry in $fullList do {
    :foreach part in $entry do {
        :local name [:pick $part 0 [:find $part "="]]
        :local value [:pick $part ([:find $part "="]+1) [:len $part]]
        :if ($name="list") do {
            :set result ($result . $value . $delimiter)
        };
    };
};
:put [:pick $result 0 ([:len $result]-[:len $delimiter])]
Just replace the first line with whichever IP you need. You could declare that variable as a global one, but in that case, you'd have to set it right before running the script.
 
KennethL
just joined
Topic Author
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

Re: Find the Address-List to which an IP is assigned

Tue Jan 31, 2012 10:03 am

Hi Again,

I know it has been a while since you posted this, and realise that I never gave credit where it was due.

Therefore, just wanted to say thanks and confirm that your script worked BRILLIANTLY.

I really appreciated your input and it really did save me a lot of time and effort.

Thanks Again.
K
 
blake
Member
Member
Posts: 426
Joined: Mon May 31, 2010 10:46 pm
Location: Arizona

Re: Find the Address-List to which an IP is assigned

Tue Jan 31, 2012 10:16 am

:put [/ip firewall address-list get value-name=list [find address=192.168.x.x]]
 
KennethL
just joined
Topic Author
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

Re: Find the Address-List to which an IP is assigned

Tue Jan 31, 2012 1:09 pm

Wow - this does the same thing..

Great stuff.. thanks Blake.

k.
 
betadineee
just joined
Posts: 1
Joined: Tue Dec 04, 2018 4:20 pm

Re: Find the Address-List to which an IP is assigned

Mon Jan 28, 2019 6:17 pm

:put [/ip firewall address-list get value-name=list [find address=192.168.x.x]]
Hi, i got some error if there is the IP on multiple list,
> :put [/ip firewall address-list get value-name=list [find address=1.1.1.1]]
invalid internal item number
how i can see all of list that contains the ip?
please help me, thx before

Who is online

Users browsing this forum: No registered users and 30 guests