Find the Address-List to which an IP is assigned
RouterOS Scripting and API

5 posts   •   Page 1 of 1
KennethL
just joined
 
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

Find the Address-List to which an IP is assigned

by KennethL » 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: 1322
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

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

by boen_robot » 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:
Code: Select all
: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.
PEAR2_Net_RouterOS(1.0.0b5) - My API client in PHP
(Karma? If you want... no pressure...)

KennethL
just joined
 
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

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

by KennethL » Tue Jan 31, 2012 11: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

by blake » Tue Jan 31, 2012 11:16 am

Code: Select all
:put [/ip firewall address-list get value-name=list [find address=192.168.x.x]]
IT consultant. Network manager. Packet junkie.
1-928-328-1509

KennethL
just joined
 
Posts: 3
Joined: Wed Oct 26, 2011 10:31 pm

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

by KennethL » Tue Jan 31, 2012 2:09 pm

Wow - this does the same thing..

Great stuff.. thanks Blake.

k.

5 posts   •   Page 1 of 1

Who is online

Users browsing this forum: Majestic-12 [Bot] and 16 guests

It is currently Mon Dec 22, 2014 4:25 pm