From 9ba98c2018733a692bdcd503195064881192fb76 Mon Sep 17 00:00:00 2001 From: Jozsef Kadlecsik Date: Fri, 7 Mar 2014 13:10:01 +0100 Subject: The bash utilities are updated --- utils/ipset_list/README.md | 11 +- utils/ipset_list/ipset_list | 426 ++++++++++++++++++++-------- utils/ipset_list/ipset_list_bash_completion | 55 ++-- 3 files changed, 350 insertions(+), 142 deletions(-) (limited to 'utils/ipset_list') diff --git a/utils/ipset_list/README.md b/utils/ipset_list/README.md index 4806252..579d3bf 100644 --- a/utils/ipset_list/README.md +++ b/utils/ipset_list/README.md @@ -1,7 +1,8 @@ ipset_list ========== -ipset set listing wrapper script +ipset set listing wrapper script written for the bash shell. +It allows you to match and display sets, headers and elements in various ways. Features: @@ -12,10 +13,13 @@ Features: - Choose a delimiter character for separating members. - Show only sets containing a specific (glob matching) header. - Arithmetic comparison on headers with an integer value. +- Arithmetic comparison on flags of the headers 'Header' field. +- Arithmetic comparison on member options with an integer value. - Match members using a globbing or regex pattern. - Suppress listing of (glob matching) sets. - Suppress listing of (glob matching) headers. - Suppress listing of members matching a glob or regex pattern. +- Suppress listing of members options. - Calculate the total size in memory of all matching sets. - Calculate the amount of matching, excluded and traversed sets. - Colorize the output. @@ -62,5 +66,10 @@ Examples: - `ipset_list -c -t -Cs -Ts -Xh "@(Size*|Re*|Header):*" -Ht "!(bitmap:*)"` - find all sets not of any bitmap type, count their members sum, display only the 'Type' header, count amount of matching and traversed sets. - `ipset_list -Co -c -Ts -Tm` - show all set names, count their members, count total amount of sets, show total memory usage of all sets, colorize the output - `ipset_list -m -r -To 0` - show members of all sets, try to resolve hosts, set the timeout to 0 (effectivly disabling it). +- `ipset_list -m -Xo setA` - show members of setA, but suppress displaying of the elements options. +- `ipset_list -m -Oi packets:0` - show members of all sets which have a packet count of 0. +- `ipset_list -m -Oi "packets:>100" -Oi "bytes:>1024"` - show members of all sets which have a packet count greater than 100 and a byte count greater than 1024. +- `ipset_list -n -Ca "foo*"` - show only set names matching the glob "foo*" and enable all counters. +- `ipset_list -Hi "markmask:>=0x0000beef" -Hi timeout:\!10000` - show only sets with the header 'Header' fields containing a markmask greater or equal to 0x0000beef and a timeout which is not 10000. diff --git a/utils/ipset_list/ipset_list b/utils/ipset_list/ipset_list index 18743a5..6db57ae 100755 --- a/utils/ipset_list/ipset_list +++ b/utils/ipset_list/ipset_list @@ -7,7 +7,7 @@ # https://sourceforge.net/projects/ipset-list/ # ----------------------------------------------------------------- -# Copyright (C) 2013 AllKind (AllKind@fastest.cc) +# Copyright (C) 2013-2014 AllKind (AllKind@fastest.cc) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -23,8 +23,9 @@ # along with this program. If not, see . # ----------------------------------------------------------------- +# Compatible with ipset version 6+ # Tested with ipset versions: -# 6.16.1 +# 6.16.1, 6.20.1 # ----------------------------------------------------------------- # ----------------------------------------------------------------- @@ -34,6 +35,8 @@ # - Choose a delimiter character for separating members. # - Show only sets containing a specific (glob matching) header. # - Arithmetic comparison on headers with an integer value. +# - Arithmetic comparison on flags of the headers 'Header' field. +# - Arithmetic comparison on member options with an integer value. # - Match members using a globbing or regex pattern. # - Suppress listing of (glob matching) sets. # - Suppress listing of (glob matching) headers. @@ -42,6 +45,7 @@ # - Calculate the amount of matching, excluded and traversed sets. # - Colorize the output. # - Operate on a single, selected, or all sets. +# - Programmable completion is included to make usage easier and faster. # ----------------------------------------------------------------- # ----------------------------------------------------------------- @@ -58,7 +62,7 @@ # $0 -m -r -s setA - show members of setA resolved and sorted # $0 -Ts - show all set names and total count of sets. # $0 -Tm - calculate total size in memory of all sets. -# $0 -Mc 0 - show sets with zero members +# $0 -Mc 0 - show sets with zero members # $0 -Fi References:0 - show all sets with 0 references # $0 -Hr 0 - shortcut for `-Fi References:0' # $0 -Xs setA -Xs setB - show all set names, but exclude setA and setB. @@ -91,7 +95,7 @@ # $0 -c -m -Xg "210.*" setA - show members of setA, but suppress listing of entries #+ matching the glob pattern "210.*", show count of excluded and total members. # -# $0 -t -Tm -Xh "@(Type|Re*|Header):*" +# $0 -t -Tm -Xh "@(Type|Re*|Header):*" #+ show all sets headers, but suppress all but name and memsize entry, #+ calculate the total memory size of all sets. # @@ -114,14 +118,39 @@ # # $0 -m -r -To 0 - show members of all sets, try to resolve hosts, # set the timeout to 0 (effectivly disabling it). +# +# $0 -m -Xo setA - show members of setA, +# + but suppress displaying of the elements options. +# +# $0 -m -Oi packets:0 +# + show members of all sets which have a packet count of 0. +# +# $0 -m -Oi "packets:>100" -Oi "bytes:>1024" +# + show members of all sets which have a +# + packet count greater than 100 and a byte count greater than 1024. +# +# $0 -n -Ca "foo*" +# + show only set names matching the glob "foo*" and enable all counters. +# +# $0 -Hi "markmask:>=0x0000beef" -Hi timeout:\!10000` +# + show only sets with the header 'Header' fields containing a markmask +# + greater or equal to 0x0000beef and a timeout which is not 10000. # ----------------------------------------------------------------- # ----------------------------------------------------------------- # Modify here # ----------------------------------------------------------------- -# path to ipset. defaults to `/sbin/ipset' if unset. -ipset="/sbin/ipset" +# modify your PATH variable +# by default the path is only set if the PATH variable is not already set in the environment +# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin +: ${PATH:=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin} + +# path to ipset. +# defaults to `/sbin/ipset' if unset. +#ipset="/sbin/ipset" +# find in path if not declared in parent environment +: ${ipset:=$(command -v ipset)} # default delimiter character for set members (elements). # defaults to whitespace if unset. @@ -136,6 +165,7 @@ TMOUT=30 colorize=0 # path to cl (to colorize the output). +# http://sourceforge.net/projects/colorize-shell/ or # https://github.com/AllKind/cl # defaults to `/usr/local/bin/cl' if unset. cl="/usr/local/bin/cl" @@ -198,18 +228,21 @@ set +u # variables export LC_ALL=C -readonly version=2.7 +readonly version=3.1 readonly me="${0//*\//}" readonly oIFS="$IFS" -declare ips_version="" str_search="" str_xclude="" opt str_hval str_op +declare ips_version="" str_search="" str_xclude="" opt str_name str_val str_op declare -i show_all=show_count=show_members=headers_only=names_only=isolate=calc_mem=count_sets=sets_total=0 -declare -i match_on_header=glob_search=regex_search=member_count=match_count=do_count=0 -declare -i exclude_header=glob_xclude_element=glob_xclude_element=exclude_set=0 -declare -i in_header=found_set=found_hxclude=found_sxclude=xclude_count=mem_total=mem_tmp=set_count=sets_sum=i=x=idx=0 -declare -a arr_sets=() arr_par=() arr_hcache=() arr_mcache=() arr_hsearch=() -declare -a arr_hsearch_int=() arr_hxclude=() arr_sxclude=() arr_match_on_msum=() +declare -i match_on_header=glob_search=regex_search=member_count=match_count=do_count=opt_int_search=0 +declare -i exclude_header=glob_xclude_element=glob_xclude_element=exclude_set=xclude_member_opts=0 +declare -i in_header=found_set=found_member_opt=found_hxclude=found_sxclude=xclude_count=mem_total=mem_tmp=set_count=sets_sum=i=x=y=idx=0 +declare -a arr_sets=() arr_par=() arr_hcache=() arr_mcache=() arr_hsearch=() arr_tmp=() +declare -a arr_hsearch_int=() arr_hsearch_xint=() arr_hxclude=() arr_sxclude=() arr_match_on_msum=() arr_opt_int_search=() +# ----------------------------------------------------------------- # functions +# ----------------------------------------------------------------- + ex_miss_optarg() { printf "%s of option \`%s' is missing\n" "$2" "$1" >&2 exit 2 @@ -224,9 +257,68 @@ is_int() { [[ $1 = +([[:digit:]]) ]] } +is_digit_or_xigit() { +[[ $1 = @(+([[:digit:]])|0x+([[:xdigit:]])) ]] +} + is_compare_str() { [[ $1 = ?(\!|<|>|<=|>=)+([[:digit:]]) ]] } + +add_search_to_member_cache() { +if ((show_members || show_all || isolate)); then + arr_mcache[i++]="$REPLY" +fi +} + +arith_elem_opt_search() { +found_member_opt=0 +for y in ${!arr_opt_int_search[@]}; do + str_val="${arr_opt_int_search[y]#*:}" + str_op="${str_val//[[:digit:]]}" # compare operator defaults to `==' + [[ ${str_op:===} = \! ]] && str_op='!=' + set -- $REPLY + shift + while (($# > 1)); do + if [[ $1 = ${arr_opt_int_search[y]%:*} ]]; then + if is_int "${str_val//[[:punct:]]}"; then + if (($2 $str_op ${str_val//[[:punct:]]})); then + let found_member_opt+=1 + shift + fi + fi + fi + shift + done +done +if ((opt_int_search == found_member_opt)); then + let match_count+=1 + add_search_to_member_cache +fi +} + +xclude_elem_search() { +if ((glob_xclude_element)); then # exclude matching members + if [[ $REPLY = $str_xclude ]]; then + let xclude_count+=1 + return 0 + fi +elif ((regex_xclude_element)); then # exclude matching members + if [[ $REPLY =~ $str_xclude ]]; then + let xclude_count+=1 + return 0 + else + if (($? == 2)); then + printf "Invalid regex pattern \`%s'.\n" "$str_xclude" >&2 + exit 1 + fi + fi +fi +return 1 +} + +# ----------------------------------------------------------------- +# main # ----------------------------------------------------------------- # validate value of colorize @@ -238,36 +330,47 @@ fi while (($#)); do case "$1" in -\?|-h) printf "\n\tipset set listing wrapper script\n\n" - printf '%s [option [opt-arg]] [set-name] [...]\n\n' "$me" - printf '%s %s\n' "$me" "{-?|-h} | -n" - printf '%s %s\n\t%s\n' "$me" "[-i|-r|-s|-Co] [-d char] [-To value]"\ - "[{-Fg|-Fr}|{-Xg|-Xr} pattern] -- set-name" - printf '%s %s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n' "$me"\ - "[-t|-c|-Ca|-Co|-Cs|-Tm|-Ts]"\ - "[-Fh header-glob:value-glob] [...]"\ - "[-Fi header-glob:[!|<|>|<=|>=]value] [...]"\ - "[-Fg|-Fr pattern] [-Ht type-glob]"\ - "[-Hr|-Hs|-Hv [!|<|>|<=|>=]value]"\ - "[-Mc [!|<|>|<=|>=]value] [...] [-To value]"\ - "[-Xh header-glob:value-glob] [...]"\ - "[-Xs setname-glob] [...] -- [set-name] [...]" - printf '%s %s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n' "$me"\ - "[-a|-c|-m|-r|-s|-Ca|-Co|-Cs|-Tm|-Ts] [-d char]"\ - "[-Fh header-glob:value-glob] [...]"\ - "[-Fi header-glob:[!|<|>|<=|>=]value] [...]"\ - "[-Fg|-Fr pattern] [-Ht type-glob]"\ - "[-Hr|-Hs|-Hv [!|<|>|<=|>=]value]"\ - "[-Mc [!|<|>|<=|>=]value] [...] [-To value]"\ + printf '%s [option [opt-arg]] [set-name-glob] [...]\n\n' "$me" + printf '%s %s\n' "$me" "{-?|-h} | -v" + printf '%s %s\n\t%s\n\t%s\n' "$me"\ + "[-i|-r|-s|-Co|-Xo] [-d char] [-To value]"\ + "[-Fg|-Fr pattern] [-Xg|-Xr pattern]"\ + "[-Oi option-glob:[!|<|>|<=|>=]value] [...] -- set-name" + printf '%s %s\n\t%s\n\t%s\n\t%s\n' "$me"\ + "[-n|-c|-Ca|-Co|-Cs|-Tm|-Ts|-Xs] [-To value]"\ + "[-Fh header-glob:value-glob] [...] [-Fg|-Fr pattern]"\ + "[-Hi glob:[!|<|>|<=|>=]value] [...]"\ + "[-Oi option-glob:[!|<|>|<=|>=]value] [...] -- [set-name-glob] [...]" + printf '%s %s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n' "$me"\ + "[-t|-c|-Ca|-Co|-Cs|-Tm|-Ts]"\ + "[-Fh header-glob:value-glob] [...]"\ + "[-Fi header-glob:[!|<|>|<=|>=]value] [...]"\ + "[-Fg|-Fr pattern] [-Ht type-glob]"\ + "[-Hi glob:[!|<|>|<=|>=]value] [...]"\ + "[-Hr|-Hs|-Hv [!|<|>|<=|>=]value]"\ + "[-Mc [!|<|>|<=|>=]value] [...] [-To value]"\ + "[-Oi option-glob:[!|<|>|<=|>=]value] [...]"\ "[-Xh header-glob:value-glob] [...]"\ - "[-Xg|-Xr pattern] [-Xs setname-glob] [...] -- [set-name] [...]" + "[-Xs set-name-glob] [...] -- [set-name-glob] [...]" + printf '%s %s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n' "$me"\ + "[-a|-c|-m|-r|-s|-Ca|-Co|-Cs|-Tm|-Ts|-Xo] [-d char]"\ + "[-Fh header-glob:value-glob] [...]"\ + "[-Fi header-glob:[!|<|>|<=|>=]value] [...]"\ + "[-Fg|-Fr pattern] [-Ht type-glob]"\ + "[-Hi glob:[!|<|>|<=|>=]value] [...]"\ + "[-Hr|-Hs|-Hv [!|<|>|<=|>=]value]"\ + "[-Mc [!|<|>|<=|>=]value] [...]"\ + "[-Oi option-glob:[!|<|>|<=|>=]value] [...]"\ + "[-To value] [-Xh header-glob:value-glob] [...]"\ + "[-Xg|-Xr pattern] [-Xs set-name-glob] [...] -- [set-name-glob] [...]" printf 'options:\n' printf '%-13s%s\n' '-a' 'show all information but with default delim (whitespace).'\ - '-c' 'calculate members and match (-Fg|-Fr) sum.'\ + '-c' 'calculate members and match sum.'\ '-d delim' 'delimiter character for separating member entries.'\ '-h|-?' 'show this help text.'\ '-i' 'show only the members of a single set.'\ '-m' 'show set members.'\ - '-n' "show set names only (raw \`ipset list -n' output)."\ + '-n' "show set names only."\ '-r' 'try to resolve ip addresses in the output (slow!).'\ '-s' 'print elements sorted (if supported by the set type).'\ '-t' 'show set headers only.'\ @@ -281,14 +384,18 @@ while (($#)); do 'show sets containing one or more [ext]glob matching headers.' printf '%s\n\t%s\n' '-Fi header-glob:[!|<|>|<=|>=]value [...]'\ 'show sets matching one or more integer valued header entries.' + printf '%s\n\t%s\n' '-Hi header-glob:[!|<|>|<=|>=]value [...]'\ + "match one or more integer valued headers \`Header' entries." printf '%-24s%s\n' '-Ht set-type-glob' 'match on set type.'\ '-Hr [!|<|>|<=|>=]value' 'match on number of references (value=int).'\ '-Hs [!|<|>|<=|>=]value' 'match on size in memory (value=int).'\ '-Hv [!|<|>|<=|>=]value' 'match on revision number (value=int).' printf '%-30s%s\n' '-Mc [!|<|>|<=|>=]value [...]' 'match on member count (value=int).' + printf '%s\n\t%s\n' '-Oi option-glob:[!|<|>|<=|>=]value [...]' 'match on member options (value=int).' printf '%-13s%s\n' '-Tm' 'calculate total memory usage of all matching sets.'\ '-To' 'set timeout value (int) for read (listing sets).'\ - '-Ts' 'count amount of traversed sets.' + '-Ts' 'count amount of traversed sets.'\ + '-Xo' 'suppress display of member options.' printf '%s\n\t%s\n' '-Xh header-glob:value-glob [...]'\ 'exclude one or more [ext]glob matching header entries.' printf '%-13s%s\n' '-Xg pattern' 'exclude members matching a [ext]glob pattern.'\ @@ -352,6 +459,15 @@ while (($#)); do str_search="$2" shift 2 ;; + -Oi) let opt_int_search+=1 + [[ $2 ]] || ex_miss_optarg $1 "pattern" + if [[ $2 = *:* ]] && is_compare_str "${2#*:}"; then + arr_opt_int_search[y++]="$2" + shift 2 + else + ex_invalid_usage "invalid format of header descriptor. expecting: \`glob:[!|<|>|<=|>=]value'" + fi + ;; -Fh) let match_on_header+=1 # show only sets, which contain a matching header entry [[ $2 ]] || ex_miss_optarg $1 "header pattern" if [[ $2 = *:* ]]; then @@ -363,13 +479,22 @@ while (($#)); do ;; -Fi) let match_on_header+=1 # show only sets, containing a matching (int compare) header entry [[ $2 ]] || ex_miss_optarg $1 "header pattern" - if is_compare_str "$2"; then + if [[ $2 = *:* ]] && is_compare_str "${2#*:}"; then arr_hsearch_int[idx++]="$2" shift 2 else ex_invalid_usage "invalid format of header descriptor. expecting: \`name:[!|<|>|<=|>=]value'" fi ;; + -Hi) let match_on_header+=1 # match on name + integer (digit & xdigit) inside of headers 'Header' flag + [[ $2 ]] || ex_miss_optarg $1 "header pattern" + if [[ $2 = *:?(\!|<|>|<=|>=)@(+([[:digit:]])|0x+([[:xdigit:]])) ]]; then + arr_hsearch_xint[${#arr_hsearch_xint[@]}]="$2" + shift 2 + else + ex_invalid_usage "invalid format of headers \'Header' flag descriptor. expecting: \`name:[!|<|>|<=|>=]value'" + fi + ;; -Hr) let match_on_header+=1 # shortcut for -Fi References:... [[ $2 ]] || ex_miss_optarg $1 "header pattern" if is_compare_str "$2"; then @@ -445,6 +570,9 @@ while (($#)); do str_xclude="$2" shift 2 ;; + -Xo) xclude_member_opts=1 # don't show elements options + shift + ;; -Xs) exclude_set=1 # don't show certain sets [[ $2 ]] || ex_miss_optarg $1 "set name ([ext]glob pattern)" arr_sxclude[${#arr_sxclude[@]}]="$2" @@ -460,7 +588,7 @@ while (($#)); do *) break esac done -declare -i i=x=idx=0 +declare -i i=x=y=idx=0 # check for ipset program and version [[ -x ${ipset:=/sbin/ipset} ]] || { @@ -486,24 +614,25 @@ fi # option logic if ((names_only)); then - if ((headers_only||show_count||show_members||show_all||isolate||\ - match_on_header||do_count||glob_search||regex_search||calc_mem||\ - glob_xclude_element||regex_xclude_element||count_sets||sets_total||exclude_set)) + if ((headers_only||show_members||show_all||isolate||\ + glob_xclude_element||regex_xclude_element||xclude_member_opts)) then - ex_invalid_usage "option -n does not allow another option" + ex_invalid_usage "option -n does not allow this combination of options" fi - # raw ipset output - "$ipset" list -n - exit $? fi if ((headers_only)); then if ((show_members || show_all || isolate)); then ex_invalid_usage "options -t and -a|-i|-m are mutually exclusive" fi fi +if ((headers_only)); then + if ((xclude_member_opts||glob_xclude_element||regex_xclude_element)); then + ex_invalid_usage "options -t and -Xg|-Xr|-Xo are mutually exclusive" + fi +fi if ((isolate)); then if ((show_count||show_all||calc_mem||count_sets||sets_total||exclude_set)); then - ex_invalid_usage "options -i and -a|-c|-Cs|-Tm|-Ts|-Xs are mutually exclusive" + ex_invalid_usage "options -i and -a|-c|-Ca|-Cs|-Tm|-Ts|-Xs are mutually exclusive" fi if ((match_on_header)); then ex_invalid_usage "option -i does not allow matching on header entries" @@ -513,9 +642,6 @@ if ((glob_search || regex_search)); then if ((glob_search && regex_search)); then ex_invalid_usage "options -Fg and -Fr are mutually exclusive" fi - if ((glob_xclude_element || regex_xclude_element)); then - ex_invalid_usage "options -Fg|-Fr and -Xg|-Xr are mutually exclusive" - fi fi if ((exclude_header)); then if ! ((headers_only || show_all)); then @@ -524,7 +650,7 @@ if ((exclude_header)); then fi if ((glob_xclude_element || regex_xclude_element)); then if ! ((show_members || show_all || isolate)); then - ex_invalid_usage "options -Fg|-Fr require any of -a|-i|-m" + ex_invalid_usage "options -Xg|-Xr require any of -a|-i|-m" fi fi if ((colorize)); then @@ -551,7 +677,7 @@ if ((colorize)); then for opt in col_fg col_bg col_headers col_members col_match col_memsize \ col_set_count col_set_total col_highlight do - ($cl ${!opt}) || ex_invalid_usage "variable \`$opt' has an invalid color value: \`${!opt}'" + ("$cl" ${!opt}) || ex_invalid_usage "variable \`$opt' has an invalid color value: \`${!opt}'" done [[ -t 1 ]] || colorize=0 # output is not a terminal fi @@ -565,10 +691,13 @@ if ! ((${#arr_sets[@]})); then exit 1 fi if [[ $1 ]]; then # there are remaining arg(s) - for opt in "$@"; do found_set=0 # check if the sets exist + for opt; do found_set=0 # check if the sets exist for idx in ${!arr_sets[@]}; do - if [[ $opt = ${arr_sets[idx]} ]]; then found_set=1 - break + if [[ ${arr_sets[idx]} = $opt ]]; then found_set=1 + # match could be a glob, thus multiple matches possible + # save to temp array + arr_tmp[${#arr_tmp[@]}]="${arr_sets[idx]}" + unset arr_sets[idx] fi done if ! ((found_set)); then @@ -580,7 +709,10 @@ if [[ $1 ]]; then # there are remaining arg(s) ex_invalid_usage "option -i is only valid for a single set" fi fi - arr_sets=("$@") # reassign remaining args + arr_sets=("${arr_tmp[@]}") # reassign matched sets + if ((isolate && ${#arr_sets[@]} > 1)); then + ex_invalid_usage "option -i is only valid for a single set" + fi else if ((isolate)); then ex_invalid_usage "option -i is only valid for a single set" @@ -589,7 +721,7 @@ fi # read sets for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() - while read -r || { + while read -r || { (($? > 128)) && \ printf "timeout reached or signal received, while reading set \`%s'.\n" \ "${arr_sets[idx]}" >&2 && continue 2; @@ -603,26 +735,32 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi let sets_sum+=1 if ((exclude_set)); then # don't show certain sets - for x in ${!arr_sxclude[@]}; do - if [[ ${arr_sets[idx]} = ${arr_sxclude[x]} ]]; then let found_sxclude+=1 + for y in ${!arr_sxclude[@]}; do + if [[ ${arr_sets[idx]} = ${arr_sxclude[y]} ]]; then let found_sxclude+=1 continue 3 # don't unset, as user could list sets multiple times fi done fi - in_header=1 found_set=1 found_header=0 member_count=0 match_count=0 xclude_count=0 mem_tmp=0 i=0 x=0 + in_header=1 found_set=1 found_header=0 member_count=0 match_count=0 xclude_count=0 mem_tmp=0 i=0 x=0 if ! ((isolate)); then # if showing members only, continue without saving any header data - if ! ((headers_only||show_members||show_all||show_count||match_on_header||do_count||calc_mem||glob_search||regex_search)) + if ((names_only)); then + if ((colorize)); then + arr_hcache[x++]="$("$cl" bold $col_headers)${REPLY#*:+([[:blank:]])}$("$cl" normal $col_fg $col_bg)" + else + arr_hcache[x++]="${REPLY#*:+([[:blank:]])}" + fi + elif ! ((headers_only||show_members||show_all||show_count||match_on_header||do_count||calc_mem||glob_search||regex_search||opt_int_search)) then - in_header=0 + in_header=0 if ((colorize)); then - arr_hcache[x++]="$($cl bold $col_headers)${REPLY}$($cl normal $col_fg $col_bg)" + arr_hcache[x++]="$("$cl" bold $col_headers)${REPLY}$("$cl" normal $col_fg $col_bg)" else arr_hcache[x++]="$REPLY" fi break # nothing to show but the names else if ((colorize)); then - arr_hcache[x++]=$'\n'"$($cl bold $col_headers)${REPLY}$($cl normal $col_fg $col_bg)" + arr_hcache[x++]=$'\n'"$("$cl" bold $col_headers)${REPLY}$("$cl" normal $col_fg $col_bg)" else arr_hcache[x++]=$'\n'"$REPLY" fi @@ -641,8 +779,8 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi fi if ((exclude_header)); then # don't show certain headers - for idx in ${!arr_hxclude[@]}; do - if [[ ${REPLY%%:*} = ${arr_hxclude[idx]%%:*} && ${REPLY#*: } = ${arr_hxclude[idx]#*:} ]] + for y in ${!arr_hxclude[@]}; do + if [[ ${REPLY%%:*} = ${arr_hxclude[y]%%:*} && ${REPLY#*: } = ${arr_hxclude[y]#*:} ]] then found_hxclude=1 break fi @@ -650,7 +788,7 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi if ((show_all && ! found_hxclude)); then if ((colorize)); then - arr_hcache[x++]="$($cl bold $col_headers)${REPLY}$($cl normal $col_fg $col_bg)" + arr_hcache[x++]="$("$cl" bold $col_headers)${REPLY}$("$cl" normal $col_fg $col_bg)" else arr_hcache[x++]="$REPLY" fi @@ -663,25 +801,56 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi if ((in_header)); then # we should be in the header if ((match_on_header && found_header < match_on_header)); then # match on an header entry - for idx in ${!arr_hsearch[@]}; do # string compare - if [[ ${REPLY%%:*} = ${arr_hsearch[idx]%%:*} && ${REPLY#*: } = ${arr_hsearch[idx]#*:} ]] + for y in ${!arr_hsearch[@]}; do # string compare + if [[ ${REPLY%%:*} = ${arr_hsearch[y]%%:*} && ${REPLY#*: } = ${arr_hsearch[y]#*:} ]] then let found_header+=1 fi done - for idx in ${!arr_hsearch_int[@]}; do # int compare - if [[ ${REPLY%%:*} = ${arr_hsearch_int[idx]%%:*} ]]; then # header name matches + for y in ${!arr_hsearch_int[@]}; do # int compare + if [[ ${REPLY%%:*} = ${arr_hsearch_int[y]%%:*} ]]; then # header name matches if ! is_int "${REPLY#*: }"; then printf "header value \`%s' is not an integer.\n" "${REPLY#*: }" >&2 exit 1 fi - str_hval="${arr_hsearch_int[idx]#*:}" - str_op="${str_hval//[[:digit:]]}" # compare operator defaults to `==' + str_val="${arr_hsearch_int[y]#*:}" + str_op="${str_val//[[:digit:]]}" # compare operator defaults to `==' [[ ${str_op:===} = \! ]] && str_op='!=' - if ((${REPLY#*: } $str_op ${str_hval//[[:punct:]]})); then + if ((${REPLY#*: } $str_op ${str_val//[[:punct:]]})); then let found_header+=1 fi fi done + # search and arithmetic compare values of the headers 'Header' flag + if ((${#arr_hsearch_xint[@]})) && [[ ${REPLY%%:*} = Header ]]; then + set -- ${REPLY#*:} + while (($#)); do + if is_digit_or_xigit "$1"; then + shift + continue + fi + for y in ${!arr_hsearch_xint[@]}; do + str_name="${arr_hsearch_xint[y]%%:*}" + str_val="${arr_hsearch_xint[y]#*:}" + if [[ $str_val = ??0x+([[:xdigit:]]) ]]; then + str_op="${str_val%0x*}" + elif [[ $str_val = ??+([[:digit:]]) ]]; then + str_op="${str_val//[[:digit:]]}" + fi + str_val="${str_val#"${str_op}"}" + [[ ${str_op:===} = \! ]] && str_op='!=' + if [[ $1 = $str_name ]]; then + if is_digit_or_xigit "$2"; then + if (($2 $str_op $str_val)); then + let found_header+=1 + shift + break + fi + fi + fi + done + shift + done + fi fi if ((calc_mem)); then if [[ ${REPLY%%:*} = "Size in memory" ]]; then @@ -695,8 +864,8 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi if ((headers_only || show_all)); then found_hxclude=0 if ((exclude_header)); then # don't show certain headers - for idx in ${!arr_hxclude[@]}; do - if [[ ${REPLY%%:*} = ${arr_hxclude[idx]%%:*} && ${REPLY#*: } = ${arr_hxclude[idx]#*:} ]] + for y in ${!arr_hxclude[@]}; do + if [[ ${REPLY%%:*} = ${arr_hxclude[y]%%:*} && ${REPLY#*: } = ${arr_hxclude[y]#*:} ]] then found_hxclude=1 break fi @@ -707,28 +876,42 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() fi fi else # this should be a member entry - if ((show_members || show_all || isolate || glob_search || regex_search)); then - if ((glob_search)); then # show sets with matching members - if [[ $REPLY = $str_search ]]; then let match_count+=1 - if ((show_members || show_all || isolate)); then - arr_mcache[i++]="$REPLY" + if ((show_members || show_all || isolate || glob_search || regex_search || opt_int_search)); then + if ((glob_search)); then # show sets with glob pattern matching members + if ! xclude_elem_search; then + if [[ $REPLY = $str_search ]]; then + if ((opt_int_search)); then + arith_elem_opt_search + else + let match_count+=1 + add_search_to_member_cache + fi fi fi - elif ((regex_search)); then # show sets with matching members - if [[ $REPLY =~ $str_search ]]; then let match_count+=1 - if ((show_members || show_all || isolate)); then - arr_mcache[i++]="$REPLY" - fi - else - if (($? == 2)); then - printf "Invalid regex pattern \`%s'.\n" "$str_search" - exit 1 + elif ((regex_search)); then # show sets with regex pattern matching members + if ! xclude_elem_search; then + if [[ $REPLY =~ $str_search ]]; then + if ((opt_int_search)); then + arith_elem_opt_search + else + let match_count+=1 + add_search_to_member_cache + fi + else + if (($? == 2)); then + printf "Invalid regex pattern \`%s'.\n" "$str_search" >&2 + exit 1 + fi fi fi + elif ((opt_int_search)); then # show sets with matching member options + if ! xclude_elem_search; then + arith_elem_opt_search + fi else if ((glob_xclude_element)); then # exclude matching members if ! [[ $REPLY = $str_xclude ]]; then - arr_mcache[i++]="$REPLY" + add_search_to_member_cache else let xclude_count+=1 fi elif ((regex_xclude_element)); then # exclude matching members @@ -736,10 +919,10 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() let xclude_count+=1 else if (($? == 2)); then - printf "Invalid regex pattern \`%s'.\n" "$str_xclude" + printf "Invalid regex pattern \`%s'.\n" "$str_xclude" >&2 exit 1 fi - arr_mcache[i++]="$REPLY" + add_search_to_member_cache fi else arr_mcache[i++]="$REPLY" @@ -755,10 +938,10 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() esac done < <("$ipset" list "${arr_sets[idx]}" "${arr_par[@]}") if ((found_set)); then # print gathered information - if ((glob_search || regex_search)) && ((match_count == 0)); then - continue # glob or regex search didn't match + if ((glob_search || regex_search || opt_int_search)) && ((match_count == 0)); then + continue # glob, regex or option-integer search didn't match fi - if ((${#arr_match_on_msum[@]} > 0)); then # match on member sum + if ((${#arr_match_on_msum[@]} > 0)); then # match on member sum (do_count=1) for i in ${!arr_match_on_msum[@]}; do str_op="${arr_match_on_msum[i]//[[:digit:]]}" [[ ${str_op:===} = \! ]] && str_op='!=' @@ -771,64 +954,69 @@ for idx in "${!arr_sets[@]}"; do found_set=0 arr_hcache=() arr_mcache=() if ((calc_mem)); then let mem_total+=$mem_tmp fi - if ((${#arr_hcache[@]})); then + if ((${#arr_hcache[@]})); then # print header if ((colorize)); then - printf "$($cl $col_headers)%b$($cl normal $col_fg $col_bg)\n" "${arr_hcache[@]}" + printf "$("$cl" $col_headers)%b$("$cl" normal $col_fg $col_bg)\n" "${arr_hcache[@]}" else printf "%s\n" "${arr_hcache[@]}" fi fi - if ((${#arr_mcache[@]})); then + if ((${#arr_mcache[@]})); then # print members + if ((xclude_member_opts)); then + arr_mcache=( "${arr_mcache[@]%% *}" ) + fi IFS="${delim:= }" if ((colorize)); then - printf "$($cl $col_members)%s$($cl normal $col_fg $col_bg)" "${arr_mcache[*]}" + printf "$("$cl" $col_members)%s$("$cl" normal $col_fg $col_bg)" "${arr_mcache[*]}" else printf "%s" "${arr_mcache[*]}" fi IFS="$oIFS" printf "\n" fi - if ((show_count)); then - if ((glob_search || regex_search)); then + if ((show_count)); then # print counters + if ((glob_search || regex_search || opt_int_search)); then if ((colorize)); then - printf "$($cl $col_match)Match count$($cl normal $col_fg $col_bg):\ - $($cl bold $col_match)%d$($cl normal $col_fg $col_bg)\n" $match_count + printf "$("$cl" $col_match)Match count$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_match)%d$("$cl" normal $col_fg $col_bg)\n" $match_count else printf "Match count: %d\n" $match_count fi fi if ((glob_xclude_element || regex_xclude_element)); then if ((colorize)); then - printf "$($cl $col_match)Exclude count$($cl normal $col_fg $col_bg):\ - $($cl bold $col_match)%d$($cl normal $col_fg $col_bg)\n" $xclude_count + printf "$("$cl" $col_match)Exclude count$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_match)%d$("$cl" normal $col_fg $col_bg)\n" $xclude_count else printf "Exclude count: %d\n" $xclude_count fi fi if ((colorize)); then - printf "$($cl bold $col_highlight)Member count$($cl normal $col_fg $col_bg):\ - $($cl bold $col_members)%d$($cl normal $col_fg $col_bg)\n" $member_count + printf "$("$cl" bold $col_highlight)Member count$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_members)%d$("$cl" normal $col_fg $col_bg)\n" $member_count else printf "Member count: %d\n" $member_count fi fi fi done + +# print global counters if ((count_sets || calc_mem || sets_total || exclude_set)); then printf "\n" if ((count_sets)); then if ((colorize)); then - printf "$($cl bold $col_highlight)Count$($cl normal $col_fg $col_bg) of all\ - $($cl bold $col_set_count)matched sets$($cl normal $col_fg $col_bg):\ - $($cl bold $col_set_count)%d$($cl normal $col_fg $col_bg)\n" $set_count + printf "$("$cl" bold $col_highlight)Count$("$cl" normal $col_fg $col_bg) of all\ + $("$cl" bold $col_set_count)matched sets$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_set_count)%d$("$cl" normal $col_fg $col_bg)\n" $set_count else printf "Count of all matched sets: %d\n" $set_count fi if ((exclude_set)); then if ((colorize)); then - printf "$($cl bold $col_highlight)Count$($cl normal $col_fg $col_bg) of all\ - $($cl bold $col_match)excluded sets$($cl normal $col_fg $col_bg):\ - $($cl bold $col_match)%d$($cl normal $col_fg $col_bg)\n" $found_sxclude + printf "$("$cl" bold $col_highlight)Count$("$cl" normal $col_fg $col_bg) of all\ + $("$cl" bold $col_match)excluded sets$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_match)%d$("$cl" normal $col_fg $col_bg)\n" $found_sxclude else printf "Count of all excluded sets: %d\n" $found_sxclude fi @@ -836,17 +1024,17 @@ if ((count_sets || calc_mem || sets_total || exclude_set)); then fi if ((sets_total)); then if ((colorize)); then - printf "$($cl bold $col_highlight)Count$($cl normal $col_fg $col_bg) of all\ - $($cl bold $col_set_total)traversed sets$($cl normal $col_fg $col_bg):\ - $($cl bold $col_set_total)%d$($cl normal $col_fg $col_bg)\n" $sets_sum + printf "$("$cl" bold $col_highlight)Count$("$cl" normal $col_fg $col_bg) of all\ + $("$cl" bold $col_set_total)traversed sets$("$cl" normal $col_fg $col_bg):\ + $("$cl" bold $col_set_total)%d$("$cl" normal $col_fg $col_bg)\n" $sets_sum else printf "Count of all traversed sets: %d\n" $sets_sum fi fi if ((calc_mem)); then if ((colorize)); then - printf "$($cl bold $col_memsize)Total memory size$($cl normal $col_fg $col_bg)\ - of all matched sets: $($cl bold $col_memsize)%d$($cl normal $col_fg $col_bg)\n" $mem_total + printf "$("$cl" bold $col_memsize)Total memory size$("$cl" normal $col_fg $col_bg)\ + of all matched sets: $("$cl" bold $col_memsize)%d$("$cl" normal $col_fg $col_bg)\n" $mem_total else printf "Total memory size of all matched sets: %d\n" $mem_total fi diff --git a/utils/ipset_list/ipset_list_bash_completion b/utils/ipset_list/ipset_list_bash_completion index e4faf7f..f92c7ab 100644 --- a/utils/ipset_list/ipset_list_bash_completion +++ b/utils/ipset_list/ipset_list_bash_completion @@ -7,7 +7,7 @@ # https://sourceforge.net/projects/ipset-list/ # ----------------------------------------------------------------- -# Copyright (C) 2013 AllKind (AllKind@fastest.cc) +# Copyright (C) 2013-2014 AllKind (AllKind@fastest.cc) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -75,15 +75,16 @@ done _ipset_list_complete() { local -i i=x=got_bashcompl=0 -local -i show_all=isolate=show_members=resolve=headers_only=set_index=0 +local -i show_all=isolate=show_members=resolve=headers_only=names_only=0 +local -i header_operation=set_index=0 local cur prev cword words str_tmp -local sets=( $("$ipset_list" -n ) ) +local sets=( $("${ipset_list:-ipset_list}" -n) ) local opts=(-- -? -a -c -d -h -i -m -n -r -s -t -v) local Copts=(-Ca -Cs -Co) -local Fopts=(-Fh -Fi -Fg -Fr) -local Hopts=(-Hr -Hs -Ht -Hv) +local Fopts=(-Fh -Fi -Fg -Fr -Oi) +local Hopts=(-Hi -Hr -Hs -Ht -Hv) local Topts=(-Tm -To -Ts) -local Xopts=(-Xh -Xg -Xr -Xs) +local Xopts=(-Xh -Xg -Xr -Xs -Xo) local arr_types=() : ${PATH:=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin} @@ -120,22 +121,23 @@ for ((i=1; i < ${#words[@]}-1; i++)); do -a) ((set_index)) && break || show_all=1 ;; -i) ((set_index)) && break || isolate=1 Copts=(-Co) ;; -m) ((set_index)) && break || show_members=1 ;; + -n) ((set_index)) && break || names_only=1 ;; -r) ((set_index)) && break || resolve=1 ;; - -t) ((set_index)) && break || headers_only=1 ;; + -t) ((set_index)) && break || headers_only=1 Xopts=(-Xh -Xs) Fopts=(${Fopts[*]/-Oi/}) ;; --) ((set_index)) && break || set_index=$((i+1)) ;; - -\?|-h|-n|-v) + -\?|-h|-v) ((set_index)) || return 0 ;; - @(-Fh|-Fi|-Xh)) ((set_index)) && break || header_operation=1 ;; + @(-Fh|-Fi|-Hi|-Xh)) ((set_index)) && break || header_operation=1 ;; *) ((set_index)) && break # options expecting an opt arg - str_tmp="@(-@(d|Fg|Fh|Fi|Fr|Ht|Hr|Hs|Hv|Mc|To|Xg|Xh|Xr|Xs))" + str_tmp="@(-@(d|Fg|Fh|Fi|Fr|Hi|Ht|Hr|Hs|Hv|Mc|Oi|To|Xg|Xh|Xr|Xs))" if [[ ${words[i-1]} = $str_tmp ]]; then continue fi # if not an option, register set index - str_tmp="@(-@(-|?|a|c|d|h|i|m|n|r|s|t|v|Ca|Cs|Co|Fg|Fh|Fi|Fr|Ht|Hr|Hs|Hv|Mc|To|Tm|Ts|Xg|Xh|Xr))" + str_tmp="@(-@(-|?|a|c|d|h|i|m|n|r|s|t|v|Ca|Cs|Co|Fg|Fh|Fi|Fr|Hi|Ht|Hr|Hs|Hv|Mc|Oi|To|Tm|Ts|Xg|Xh|Xo|Xr))" if [[ ${words[i]} != $str_tmp ]]; then for x in ${!sets[@]}; do if [[ ${sets[x]} = ${words[i]} ]]; then @@ -148,7 +150,12 @@ for ((i=1; i < ${#words[@]}-1; i++)); do done # invalid combinations of options -if ((headers_only)); then +if ((names_only)); then + if ((headers_only)); then + return 0 + fi +fi +if ((headers_only||names_only)); then if ((show_all || show_members || isolate || resolve)); then return 0 fi @@ -169,7 +176,7 @@ if ((set_index)); then # and also handles those who have the name of ipset_list options _ipset_list_show_sets else -if [[ $prev = @(-@(\?|d|h|n|v|Fg|Fi|Fr|Ht|To|Xg|Xr)) ]]; then +if [[ $prev = @(-@(\?|d|h|v|Fg|Fi|Fr|Hi|Ht|Oi|To|Xg|Xr)) ]]; then return 0 elif [[ $prev = -Xs ]]; then # list sets if user does not want to enter a glob @@ -217,7 +224,7 @@ elif [[ $prev = @(-@(Hr|Hs|Hv|Mc)) ]]; then elif [[ $cur = -* ]]; then # any option is requested case "$prev" in - @(-@(\?|d|h|n|v|Fg|Fh|Fi|Fr|Ht|Hr|Hs|Hv|Mc|To|Xg|Xh|Xr))) + @(-@(\?|d|h|v|Fg|Fh|Fi|Fr|Hi|Ht|Hr|Hs|Hv|Mc|Oi|To|Xg|Xh|Xr))) # options that exclude any other option, # or need a value we can't predict return 0 @@ -225,18 +232,22 @@ elif [[ $cur = -* ]]; then esac # these options don't allow any other if ((${#words[@]} > 2)); then - opts=("${opts[@]/@(-n|-h|-\?)/}") + opts=("${opts[@]/@(-v|-h|-\?)/}") fi # some options allow only a subset of other options if ((isolate)); then - COMPREPLY=( $(compgen -W '-- -Co -d -r -s' -- $cur ) ) + COMPREPLY=( $(compgen -W '-- -Co -d -r -s -Fg -Fr -Oi -To -Xg -Xo -Xr' -- $cur ) ) + elif ((names_only)); then + COMPREPLY=( $(compgen -W \ + '-- -c ${Copts[@]} ${Fopts[@]} ${Hopts[@]} -Mc ${Topts[@]} -Xs' \ + -- $cur ) ) elif ((headers_only)); then COMPREPLY=( $(compgen -W \ '-- -c ${Copts[@]} ${Fopts[@]} ${Hopts[@]} -Mc ${Topts[@]} ${Xopts[@]}' \ -- $cur ) ) elif ((show_members)); then COMPREPLY=( $(compgen -W \ - '-- -c -d -r -s ${Copts[@]} ${Fopts[@]} ${Hopts[@]} -Mc ${Topts[@]}' \ + '-- -c -d -r -s ${Copts[@]} ${Fopts[@]} ${Hopts[@]} -Mc ${Topts[@]} -Xg -Xr -Xo' \ -- $cur ) ) elif ((show_all)); then COMPREPLY=( $(compgen -W \ @@ -261,13 +272,13 @@ elif [[ $cur = -* ]]; then # mutual exclusive options for ((i=1; i < x; i++)); do case "${words[i]}" in - -Fg) _ipset_list_remove_reply_entry "-Fr" "-Xg" "-Xr" ;; - -Fr) _ipset_list_remove_reply_entry "-Fg" "-Xg" "-Xr" ;; - -Xg) _ipset_list_remove_reply_entry "-Fg" "-Fr" "-Xr" ;; - -Xr) _ipset_list_remove_reply_entry "-Fg" "-Fr" "-Xg" ;; + -Fg) _ipset_list_remove_reply_entry "-Fr" ;; + -Fr) _ipset_list_remove_reply_entry "-Fg" ;; + -Xg) _ipset_list_remove_reply_entry "-Xr" ;; + -Xr) _ipset_list_remove_reply_entry "-Xg" ;; esac # options allowed multiple times - if [[ ${words[i]} = @(""|-|-@(Fh|Fi|Mc|Xh|Xs)) ]]; then + if [[ ${words[i]} = @(""|-|-@(Fh|Fi|Hi|Mc|Oi|Xh|Xs)) ]]; then continue else # remove dupe _ipset_list_remove_reply_entry "${words[i]}" -- cgit v1.2.3