Community discussions

MikroTik App
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Topic Author
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

How to delete a list in reverse order?

Sat May 02, 2020 3:34 pm

Hi, I'm an experienced programmer, but newbie to RouterOS scripting.
I want to delete all entries under "/ip firewall filter/" in reverse order.

One method is by "remove numbers=55,54,53, ....", but this is of course too much work.

I wanted to do it in a reverse for-loop like this, but it brings an error:
/ip/firewall/filter
/for i from=55 to=1 do={ remove numbers=$i; }
bad command name remove (line 1 column 26)

So, what is the best method to delete such a list?

Thx
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Topic Author
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

Re: How to delete a list in reverse order?  [SOLVED]

Sat May 02, 2020 3:46 pm

Solved as folows:
/ip/firewall/filter
:for i from=55 to=1 do={ remove numbers=$i }
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to delete a list in reverse order?

Sat May 02, 2020 5:09 pm

Can you explain why you are doing this. I would not have dared to delete any access list (filter rule) just by number.
If this are temporary access list I would have used some form of naming, and deleted them by name.
This way script can not by accident delete important rules, and in worst case prevent access to it.
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Topic Author
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

Re: How to delete a list in reverse order?

Sat May 02, 2020 6:09 pm

Can you explain why you are doing this. I would not have dared to delete any access list (filter rule) just by number.
If this are temporary access list I would have used some form of naming, and deleted them by name.
This way script can not by accident delete important rules, and in worst case prevent access to it.
I'm putting all the firewall rules into a text file named firewall.rsc that I edit on my laptop, then upload it to my MT switch/router, and there I just use the command "/import firewall.rsc". BUT: the import does an append to the currently existing rules list. So, before importing I have to clear the whole list (except the built-in rule 0, which cannot be deleted).
Of ccourse I have first to check how many rules are in the old list, to pass that number to the above script/batch command that I use in the CLI directly, ie. not from a script...
Ie.: There are many roads to Rome, or: There's more than one way to skin a cat :-)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to delete a list in reverse order?

Sat May 02, 2020 6:29 pm

I can see you can do this as a test, but in a production router I have not done any thing like this.
Make a good configuration and let it stay.
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Topic Author
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

Re: How to delete a list in reverse order?

Sat May 02, 2020 7:54 pm

I can see you can do this as a test, but in a production router I have not done any thing like this.
Make a good configuration and let it stay.
Of course that is the final goal. Currently just learning and testing the device and its software, and also doing some experimenting, all in a non-productive test environment of course.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to delete a list in reverse order?

Mon Aug 09, 2021 3:37 am

I'm putting all the firewall rules into a text file named firewall.rsc that I edit on my laptop, then upload it to my MT switch/router, and there I just use the command "/import firewall.rsc".
BUT: the import does an append to the currently existing rules list.
So, before importing I have to clear the whole list (except the built-in rule 0, which cannot be deleted).
Of ccourse I have first to check how many rules are in the old list, to pass that number to the above script/batch command that I use in the CLI directly, ie. not from a script...
Ie.: There are many roads to Rome, or: There's more than one way to skin a cat :-)

You have defined yourself as an expert programmer, I would have expected a better logical analysis in an attempt to solve the problem,
such as putting the deletion of all the rules and immediately add the indispensable firewall rule and then all the others, directly inside the firewall.rsc file...

Probably you use 7 beta, is the wrong forum section, all non-beta are for 6.x only
But,
for example, set a comment on rule(s) like "KEEP this rule becaue...." and remove all not have uppercase KEEP in any part of the comment...
Can have on this way multiple KEEP rules on any position...
/ip firewall filter
remove [find where (!(comment~"KEEP"))]
you also can add the code on top of your firewall.rsc
Last edited by rextended on Mon Aug 09, 2021 4:05 am, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to delete a list in reverse order?

Mon Aug 09, 2021 3:55 am

After I have provided a better way on previous post, not for programming, but how understand and solve the problem...

Solved as folows:
/ip/firewall/filter
:for i from=55 to=1 do={ remove numbers=$i }
I have first to check how many rules are in the old list, to pass that number to the above script

You haven't solved anything...

How to delete a list in reverse order (except the last)?

Your nonsense at least "fixed", but is for v7 and i do not have any machine to try at the moment, but can be used as example:
/ip/firewall/filter
:for i from=[:len [find]] to=1 do={ remove numbers=$i }

Who is online

Users browsing this forum: Bing [Bot] and 27 guests