I am trying to come up with a script, that adds 2 new filter rules, and after that makes sure the “drop” rule is moved to the end.
In this script I assume that what ever is at the end of the filter list BEFORE I add my rules, must be the drop rule. So I determine the index of that rule first.
However, it appears that the move command ignores the content of the $dropruleindex variable, even though that variables contains the correct index-number. What am I doing wrong? Any other way to achieve this?
Thanks for the reply! That depends how you do it, maybe that holds a hint for me how it could be done in a different way than the approach I am currently using.
Thanks for the replies. I know the “trick” with setting a comment for the rules, and reference them by their comment name. That works just fine.
However, my goal is to come up with a script that adds those rules right after the first time (self) configuration of the router. At this point all the comments for the filter rules are “default configuration”. I want to avoid having to manually set a comment named “drop” for the drop rule, before I run my script.
I am just a little surprised that the move command does not work when using variables. Presume the “drop” rule would be #3 in the list:
This WORKS:
### this will move number 3 to the end of the filter list
/ip firewall filter move 3
I have no experience with adding them, but it may be the move parameter.
Have you tried adding the rule with the “place-before=X” parameter?
X is the line number you want the rule above in the list.
No promises. I haven’t tried it!
In 4.5 move uses the “numbers” and “destination” parameters. But anyway, that does not work either.
However, it gets even more weird. For the “destination” parameter passing a variable seems to work! It’s just that for the “numbers” parameter passing a variable does not work. Btw, with “not work” I mean it fails with “no such item”.
It looks like move works with the internal .id and numeric only values (at least from my testing). This worked for me on v4.5:
Your code revised:
/ip firewall filter
add action=accept chain=input comment="VPN1" disabled=yes protocol=ipsec-esp
add action=accept chain=input comment="VPN2" disabled=yes protocol=udp src-port=500
:local fRules
# get current rule set
:set fRules [/ip firewall filter find]
# since we added two rules, move the last two rules up two positions (before last rule)
move [:pick $fRules ([:len $fRules] - 1)] [:tonum ([:len $fRules] - 3)]
move [:pick $fRules ([:len $fRules] - 2)] [:tonum ([:len $fRules] - 3)]
Possibly a better approach: You could simply move all ‘action=drop’ rules to the bottom
The below script will work whether it finds 0, 1, or more drop rules, it will move each of them to the last position in the filter table.