Community discussions

MikroTik App
 
elico
newbie
Topic Author
Posts: 34
Joined: Mon Nov 07, 2016 3:23 am

Removing ip addresses in a list based on another

Sat Dec 12, 2020 11:49 pm

I have a set of lists I want to cleanup a specific IP from another address list.
It's not working.. What am I doing wrong?
:local lists {"test1"; "test2"; "test3";};

:foreach ip in=[/ip firewall address-list find where list="CLEANUP"] do={
    :local ipAddresss [/ip firewall address-list get $ip address];
    :foreach list in=$lists do={
        /log info "$ipAddresss";
        /log info "$list";
        /ip firewall remove [/ip firewall address-list [find list=$list address=$ipAddress]]
    }
}

/log info "Ended Script";
 
Sob
Forum Guru
Forum Guru
Posts: 6463
Joined: Mon Apr 20, 2009 9:11 pm

Re: Removing ip addresses in a list based on another

Sun Dec 13, 2020 5:03 am

There's no "/ip firewall remove". This works for me:
/ip firewall address-list remove [/ip firewall address-list find list="test" address="1.2.3.4"]
but only when I write list name and address like this, I can't find a way how to make it work with variables. I assume it's possible, somehow, but if I ever understand RouterOS scripting, it will be a miracle.
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
 
elico
newbie
Topic Author
Posts: 34
Joined: Mon Nov 07, 2016 3:23 am

Re: Removing ip addresses in a list based on another

Thu Dec 17, 2020 2:22 pm

There's no "/ip firewall remove". This works for me:
/ip firewall address-list remove [/ip firewall address-list find list="test" address="1.2.3.4"]
but only when I write list name and address like this, I can't find a way how to make it work with variables. I assume it's possible, somehow, but if I ever understand RouterOS scripting, it will be a miracle.
I asusme it's lua scripting because many work with this but still...
I will try..
 
Sob
Forum Guru
Forum Guru
Posts: 6463
Joined: Mon Apr 20, 2009 9:11 pm

Re: Removing ip addresses in a list based on another

Thu Dec 17, 2020 7:11 pm

It's not Lua, it's MikroTik's custom thing (they briefly had Lua in some beta version, but it didn't make it to final). It wouldn't be that bad, it's slightly unintuitive, but it would be possible to get used to it. Main problem is that it doesn't have any useful feedback, typo in code means silent death, without any indication what's wrong. And then there's things like this. I usually have no problem doing small things even in programming languages I don't know, but not with RouterOS scripting, I get stuck all the time.

If I try simplified script:
:local list "test1";
:local ip "1.0.0.0";
:log info $list
:log info $ip
/log info [/ip firewall address-list find list="test1" address="1.0.0.0"];
/log info [/ip firewall address-list find list="test1" address=$ip];
/log info [/ip firewall address-list find list=$list address="1.0.0.0"];
/log info [/ip firewall address-list find list=$list address=$ip];
then I get:
18:04:43 script,info test1 
18:04:43 script,info 1.0.0.0 
18:04:43 script,info *5 
18:04:43 script,info *5 
18:04:43 script,info *5;*7;*8;*b 
18:04:43 script,info *5;*7;*8;*b
So variables have expected values and *5 is correct id for item I'm searching for. It means that second command with address=$ip works fine too. But next two find 1.0.0.0 in all address lists, so there's some problem with list=$list. But what could it be?
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
 
User avatar
eworm
Forum Veteran
Forum Veteran
Posts: 738
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Removing ip addresses in a list based on another

Sat Dec 19, 2020 12:03 am

But next two find 1.0.0.0 in all address lists, so there's some problem with list=$list. But what could it be?
Have a look at this commit, it explains the issue:
https://git.eworm.de/cgit/routeros-scri ... 8bbde9651a

Short conclusion: You are safe if your variable names are not lower case.
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
Sob
Forum Guru
Forum Guru
Posts: 6463
Joined: Mon Apr 20, 2009 9:11 pm

Re: Removing ip addresses in a list based on another

Sat Dec 19, 2020 1:52 am

You're right. It's actually documented:

https://wiki.mikrotik.com/wiki/Manual:S ... able_names

So on one hand I can't complain, but on the other it confirms what I'm saying, this whole thing is not intuitive (to me at least), because who would expect all property names to be reserved as variable names? :)
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
 
User avatar
eworm
Forum Veteran
Forum Veteran
Posts: 738
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Removing ip addresses in a list based on another

Sat Dec 19, 2020 11:22 am

Ah, did not know it is documented... Found it the hard way myself. 😆
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
elico
newbie
Topic Author
Posts: 34
Joined: Mon Nov 07, 2016 3:23 am

Re: Removing ip addresses in a list based on another

Mon Dec 28, 2020 5:03 pm

OK Now I got it.
I will try it later and see how it goes.

Who is online

Users browsing this forum: No registered users and 44 guests