Re: list elements of set

"Kerin Millar" <[email protected]> Sat, 27 Sep 2025 11:59:54 +0100
Newsgroups gmane.comp.security.firewalls.netfilter.general
Message-ID <[email protected]>
On Sat, 27 Sep 2025, at 11:42 AM, 0x1 | nftables wrote:
> Thank you for response and deeper insight.
>
> the following command seems to work:
>
> sudo nft 'list set basetable mx_bans_cidr'

Note that you omit the family here but nft(8) assumes "ip" in its absence.

>
> output:
>
> table ip basetable {
> 	set mx_bans_cidr {
> 		type ipv4_addr
> 		flags interval
> 		counter
> 		auto-merge
> 		elements = { 1.1.1.0/24 counter packets 0 bytes 0, 2.2.2.0/24 counter
> packets 0 bytes 0, etc ...
> }
>
>
>
> The output is a bit "verbose". Is it possible to retrieve only the
> ip/cidr elements without the "table ip basetable { ... " 
> lines?
>
> I still have to use cut, sed or awk to retrieve the ip ranges only.
>
> Ideally I would only get:
>
> 1.1.1.0/24
> 2.2.2.0/24

You could parse JSON with the popular jq utility, or some other JSON parser of your choosing.

# nft -j 'list set basetable mx_bans_cidr' |
jq -r '.nftables[1].set.elem | .[].elem.val.prefix | [ .addr, .len ] | join("/")'

-- 
Kerin Millar