Community discussions

MikroTik App
 
rplTool
just joined
Topic Author
Posts: 2
Joined: Sun Jul 26, 2020 10:18 am

Loop through submenus

Sun Jul 26, 2020 10:35 am

Hi all,

Is it possible to loop through different submenus? For example I use the following commands:
/ip firewall filter reset-counters-all
/ip firewall nat    reset-counters-all
/ip firewall mangle reset-counters-all
/ip firewall raw    reset-counters-all
Is it possible to do something like this?
/ip firewall
:foreach table in={"filter"; "nat"; "mangle"; "raw"} do={
    $table reset-counters-all
}
The above script doesn't matter, it doesn't even throw an error. Is it possible to change this script to work as expected or is it generally not possible to loop through submenus?

Regards,
Reto
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7044
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Loop through submenus  [SOLVED]

Mon Jul 27, 2020 10:02 am

No, you cannot use variable values as menu names or parameter/variable names.

One workaround is to use :parse
 
rplTool
just joined
Topic Author
Posts: 2
Joined: Sun Jul 26, 2020 10:18 am

Re: Loop through submenus

Mon Jul 27, 2020 2:18 pm

@mtz: Thanks for the advice!

Based on the hint, I came to the following solution, which is not very nice, but does the job:
:foreach table in={"filter"; "nat"; "mangle", "raw"} do={
    [:parse "/ip firewall $table reset-counters-all"]
}

But there is a problem with this solution when it comes to regex:
:foreach table in={"filter"; "nat"} do={
    [:parse "/ip firewall $table remove [find comment~\"vXXX$\"]"]
}
The dollar sign
$
generates an error. I tried to escape it with a backslash
\
, but it didn't help.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Loop through submenus

Tue Jul 28, 2020 8:10 am

Why do you need to delete these filter/nat rules?
Post example comments.

Who is online

Users browsing this forum: No registered users and 23 guests