All of lore.kernel.org
 help / color / mirror / Atom feed
* testing if a named set exists?
@ 2021-10-02  1:16 Matt Zagrabelny
  2021-10-02  2:52 ` Duncan Roe
  2021-10-02  2:57 ` Kerin Millar
  0 siblings, 2 replies; 5+ messages in thread
From: Matt Zagrabelny @ 2021-10-02  1:16 UTC (permalink / raw)
  To: netfilter

Hello,

I'd like to do something like the following:

if exists $named_set
    nft add rule ip filter output ip daddr $named_set accept
else
    nft add rule ip filter output ip daddr $default_set accept

Does anyone know if I can accomplish this with nftables?

Thanks,

-m

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: testing if a named set exists?
  2021-10-02  1:16 testing if a named set exists? Matt Zagrabelny
@ 2021-10-02  2:52 ` Duncan Roe
  2021-10-02  2:57 ` Kerin Millar
  1 sibling, 0 replies; 5+ messages in thread
From: Duncan Roe @ 2021-10-02  2:52 UTC (permalink / raw)
  To: Matt Zagrabelny; +Cc: Netfilter

On Fri, Oct 01, 2021 at 08:16:17PM -0500, Matt Zagrabelny wrote:
> Hello,
>
> I'd like to do something like the following:
>
> if exists $named_set
>     nft add rule ip filter output ip daddr $named_set accept
> else
>     nft add rule ip filter output ip daddr $default_set accept
>
> Does anyone know if I can accomplish this with nftables?
>
> Thanks,
>
> -m
How about

> if nft list ruleset | grep -q "$named_set"; then
>   nft add rule ip filter output ip daddr $named_set accept
> else
>   nft add rule ip filter output ip daddr $default_set accept
> fi

You can restrict the search to a table, e.g. instead of "ruleset"
put "table $my_table"

Cheers ... Duncan.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: testing if a named set exists?
  2021-10-02  1:16 testing if a named set exists? Matt Zagrabelny
  2021-10-02  2:52 ` Duncan Roe
@ 2021-10-02  2:57 ` Kerin Millar
  2021-10-02 11:50   ` Matt Zagrabelny
  1 sibling, 1 reply; 5+ messages in thread
From: Kerin Millar @ 2021-10-02  2:57 UTC (permalink / raw)
  To: Matt Zagrabelny; +Cc: netfilter

On Fri, 1 Oct 2021 20:16:17 -0500
Matt Zagrabelny <mzagrabe@d.umn.edu> wrote:

> Hello,
> 
> I'd like to do something like the following:
> 
> if exists $named_set
>     nft add rule ip filter output ip daddr $named_set accept
> else
>     nft add rule ip filter output ip daddr $default_set accept
> 
> Does anyone know if I can accomplish this with nftables?
> 
> Thanks,
> 
> -m

The output of nft isn't particularly amenable to parsing unless it is instructed to produce JSON. The simplest way is to act upon the exit status value of a list set command.

if nft --terse list set ip filter "$named_set" >/dev/null 2>&1; then ...

-- 
Kerin Millar

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: testing if a named set exists?
  2021-10-02  2:57 ` Kerin Millar
@ 2021-10-02 11:50   ` Matt Zagrabelny
  2021-10-02 23:50     ` Kerin Millar
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Zagrabelny @ 2021-10-02 11:50 UTC (permalink / raw)
  To: netfilter

Hey Kerin (and Duncan),

Thanks for the replies.

On Fri, Oct 1, 2021 at 9:57 PM Kerin Millar <kfm@plushkava.net> wrote:
>
> On Fri, 1 Oct 2021 20:16:17 -0500
> Matt Zagrabelny <mzagrabe@d.umn.edu> wrote:
>
> > I'd like to do something like the following:
> >
> > if exists $named_set
> >     nft add rule ip filter output ip daddr $named_set accept
> > else
> >     nft add rule ip filter output ip daddr $default_set accept
> >
> > Does anyone know if I can accomplish this with nftables?
> >
>
> The output of nft isn't particularly amenable to parsing unless it is instructed to produce JSON. The simplest way is to act upon the exit status value of a list set command.
>
> if nft --terse list set ip filter "$named_set" >/dev/null 2>&1; then ...

I should have been more specific...

I'm hoping to do this all in nft without hitting the shell. For
example, from "man bash" we have:

       ${parameter:-word}
              Use Default Values.  If parameter is unset or null, the
expansion of word is  substituted.   Otherwise,
              the value of parameter is substituted.

I was hoping for some sort of similar mechanism in nft. Like:

nft add rule ip filter output ip daddr
${named_set_does_not_exist:-default_named_set} accept

Thanks for the help!

-m

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: testing if a named set exists?
  2021-10-02 11:50   ` Matt Zagrabelny
@ 2021-10-02 23:50     ` Kerin Millar
  0 siblings, 0 replies; 5+ messages in thread
From: Kerin Millar @ 2021-10-02 23:50 UTC (permalink / raw)
  To: Matt Zagrabelny; +Cc: netfilter

On Sat, 2 Oct 2021 06:50:35 -0500
Matt Zagrabelny <mzagrabe@d.umn.edu> wrote:

> Hey Kerin (and Duncan),
> 
> Thanks for the replies.
> 
> On Fri, Oct 1, 2021 at 9:57 PM Kerin Millar <kfm@plushkava.net> wrote:
> >
> > On Fri, 1 Oct 2021 20:16:17 -0500
> > Matt Zagrabelny <mzagrabe@d.umn.edu> wrote:
> >
> > > I'd like to do something like the following:
> > >
> > > if exists $named_set
> > >     nft add rule ip filter output ip daddr $named_set accept
> > > else
> > >     nft add rule ip filter output ip daddr $default_set accept
> > >
> > > Does anyone know if I can accomplish this with nftables?
> > >
> >
> > The output of nft isn't particularly amenable to parsing unless it is instructed to produce JSON. The simplest way is to act upon the exit status value of a list set command.
> >
> > if nft --terse list set ip filter "$named_set" >/dev/null 2>&1; then ...
> 
> I should have been more specific...
> 
> I'm hoping to do this all in nft without hitting the shell. For
> example, from "man bash" we have:
> 
>        ${parameter:-word}
>               Use Default Values.  If parameter is unset or null, the
> expansion of word is  substituted.   Otherwise,
>               the value of parameter is substituted.
> 
> I was hoping for some sort of similar mechanism in nft. Like:
> 
> nft add rule ip filter output ip daddr
> ${named_set_does_not_exist:-default_named_set} accept

I see. As far as I'm aware, no such feature exists in nft at the current time.

-- 
Kerin Millar

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-02 23:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-02  1:16 testing if a named set exists? Matt Zagrabelny
2021-10-02  2:52 ` Duncan Roe
2021-10-02  2:57 ` Kerin Millar
2021-10-02 11:50   ` Matt Zagrabelny
2021-10-02 23:50     ` Kerin Millar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.