Linux Netfilter discussions
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: "U.Mutlu" <um@mutluit.com>
Cc: Florian Westphal <fw@strlen.de>, netfilter@vger.kernel.org
Subject: Re: [nftables/nft] nft equivalent of "ipset test"
Date: Wed, 18 Oct 2023 11:23:05 +0200	[thread overview]
Message-ID: <ZS+j+fCU9vehzJqY@calendula> (raw)
In-Reply-To: <652F0C75.8010006@mutluit.com>

On Wed, Oct 18, 2023 at 12:36:37AM +0200, U.Mutlu wrote:
> Florian Westphal wrote on 10/18/23 00:05:
> > U.Mutlu <um@mutluit.com> wrote:
> > > Florian Westphal wrote on 10/17/23 23:35:
> > > > U.Mutlu <um@mutluit.com> wrote:
> > > > > The "ipset" commandline tool has the "test" command
> > > > > for testing whether a given item (ie. an IP) is in a given set.
> > > > > Is there an equivalent for the "nft" commandline tool of nftables?
> > > > > I unfortunately couldn't find the answer in the manpage of nft.
> > > > 
> > > > nft "get element inet tablename setname { 1.2.3.4 }"
> > > 
> > > But isn't that printing the whole item on stdout?
> > > I just need to quickly test it only,
> > > ie. need just a return code of 0 or 1, or so,
> > > for use in a shell script (bash).
> > 
> > ?
> > 
> > nft "get element inet t s { 1.2.3.4 }" > /dev/null 2>&1; echo $?
> > 1
> > nft "add element inet t s { 1.2.3.4 }"
> > nft "get element inet t s { 1.2.3.4 }" > /dev/null 2>&1; echo $?
> > 0
> 
> Actualy I need to do this monster:   :-)
> 
> IP="1.2.3.4"
> ! nft "get element inet mytable myset  { $IP }" > /dev/null 2>&1 && \
> ! nft "get element inet mytable myset2 { $IP }" > /dev/null 2>&1 && \
>   nft "add element inet mytable myset  { $IP }"

Use 'nft create element' if you want to fail if element already
exists.

> Ie. add it to the set myset only if it's not already present in any of myset
> and myset2.
> 
> A true "test" command w/o any output, much like in "ipset test", would be a
> better method, IMO.
> 
> I've not switched yet to nftables, just (dry-) evaluating it.

  reply	other threads:[~2023-10-18  9:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17 17:11 [nftables/nft] nft equivalent of "ipset test" U.Mutlu
2023-10-17 21:35 ` Florian Westphal
2023-10-17 21:55   ` U.Mutlu
2023-10-17 22:05     ` Florian Westphal
2023-10-17 22:36       ` U.Mutlu
2023-10-18  9:23         ` Pablo Neira Ayuso [this message]
     [not found]         ` <20231017200057.57cfce21@playground>
2023-10-18  9:36           ` Pablo Neira Ayuso
2023-10-18  9:54             ` U.Mutlu
2023-10-18 10:00               ` Pablo Neira Ayuso
2023-10-18 11:07                 ` U.Mutlu
2023-10-18 11:49                   ` Pablo Neira Ayuso
2023-10-18 13:03                     ` U.Mutlu
2023-10-18 14:37                   ` Phil Sutter
2023-10-18 11:54               ` Kerin Millar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZS+j+fCU9vehzJqY@calendula \
    --to=pablo@netfilter.org \
    --cc=fw@strlen.de \
    --cc=netfilter@vger.kernel.org \
    --cc=um@mutluit.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox