All of lore.kernel.org
 help / color / mirror / Atom feed
From: Born Without <blackhole@airpost.net>
To: "netfilter@vger.kernel.org" <netfilter@vger.kernel.org>
Subject: Wrapper script for ipset listing
Date: Sat, 05 Jan 2013 17:05:50 +0100	[thread overview]
Message-ID: <50E84F5E.8060704@airpost.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 358 bytes --]

Hello list!

As I was missing those features in the ipset set listing capabilities:

- show sum of set members
- suppress listing of headers
- choose a delimiter character for separating member entries

I wrote a little wrapper script (for the bash shell) to support them.
For those who like, you'll find it attached.

Comments etc. welcome...

Best regards

[-- Attachment #2: ipset_list.bash --]
[-- Type: text/plain, Size: 2089 bytes --]

#!/bin/bash

# -----------------------------------------------------------------
ipset="/sbin/ipset"
delim=" "
TMOUT=30
# -----------------------------------------------------------------

set -f
shopt -s extglob

show_count=0 show_members=0 headers_only=0 names_only=0 i=0

[[ -x $ipset ]] || {
	printf "ipset binary \`%s' does not exist, or is not executable" "$ipset"
	exit 1
}
while (($#)); do
	case "$1" in
		-h) printf "%s [-{c|h|m|n|r|s|t}] [...] [-d char] [set-name] [...]\n" "${0//*\//}"
			exit 0
		;;
		-c) show_count=1
			shift
		;;
		-m) show_members=1
			shift
		;;
		-n) names_only=1
			shift
		;;
		-t) headers_only=1
			shift
		;;
		-d) if [[ -z $2 ]]; then
				printf "delim character is missing\n" >&2
				exit 2
			else
				if ((${#2} > 1)); then
					printf "only one character is allowed as delim\n" >&2
					exit 2
				fi
				delim="$2"
				shift 2
			fi
		;;
		-s|-r) arr_par[i++]="$1"
			shift
		;;
		-o) if [[ $2 != plain ]]; then
				printf "only plain output is supported\n" >&2
				exit 2
			else
				shift 2
			fi
		;;
		-\!|-f) shift
		;;
		*) break
	esac
done
if ((names_only)); then
	if ((show_count || show_members)); then
		printf "options -n and -c|-m are mutually exclusive\n" >&2
		exit 2
	fi
	"$ipset" l -n
	exit $?
fi
if ((headers_only)); then
	if ((show_count || show_members)); then
		printf "options -t and -c|-m are mutually exclusive\n" >&2
		exit 2
	fi
	"$ipset" l -t
	exit $?
fi

i=0
if [[ $1 ]]; then
	arr_opts=("$@")
else
	while IFS=$'\n' read -r; do
		arr_opts[i++]="$REPLY"
	done < <("$ipset" l -n)
	i=0
fi

for x in "${!arr_opts[@]}"; do
	while read -r; do
		if [[ $REPLY = Name:* ]]; then i=0
			printf "\n%s\n" "$REPLY"
			continue
		elif [[ $REPLY = @(Type|Revision|Header|Size in memory|References|Members):* ]]; then
			continue
		elif [[ -z $REPLY ]]; then
			continue
		else
			if ((show_members)); then
				printf "%s$delim" "$REPLY"
			fi
			let i+=1
		fi
	done < <("$ipset" l "${arr_opts[x]}" "${arr_par[@]}")
	if ((show_members)); then
		printf "\n"
	fi
	if ((show_count && i)); then
		printf "Member count: %d\n" $i
	fi
done

             reply	other threads:[~2013-01-05 16:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-05 16:05 Born Without [this message]
2013-01-05 16:16 ` Wrapper script for ipset listing Born Without
2013-01-06  4:54   ` Born Without
2013-01-05 17:10 ` Jan Engelhardt
2013-01-06  3:50   ` Born Without
2013-01-06 20:06     ` Jan Engelhardt
2013-01-07  7:59       ` Jozsef Kadlecsik
2013-01-09  6:52         ` Born Without
2013-01-09 11:52           ` Pablo Neira Ayuso
2013-01-10 12:53             ` Born Without
2013-01-10 15:01               ` Eliezer Croitoru
2013-01-10 21:18                 ` Born Without
2013-01-10 15:19               ` Jozsef Kadlecsik
2013-01-10 21:15                 ` Born Without
2013-01-10 21:37                   ` Born Without
2013-01-11  8:19             ` Born Without
2013-01-21  8:31             ` Born Without
2013-01-21  8:55               ` Jozsef Kadlecsik

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=50E84F5E.8060704@airpost.net \
    --to=blackhole@airpost.net \
    --cc=netfilter@vger.kernel.org \
    /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 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.