From abc4cb955dcdff89a4847bfcc70d2691b51193f8 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Fri, 3 Jul 2015 08:29:22 +0400 Subject: [PATCH] =?UTF-8?q?dumpclients:=20possibilit=C3=A9=20de=20choisir?= =?UTF-8?q?=20les=20champs=20suppl=C3=A9mentaires=20=C3=A0=20afficher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dumpclients | 59 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/dumpclients b/dumpclients index 1cca204..a28a3f8 100755 --- a/dumpclients +++ b/dumpclients @@ -19,15 +19,18 @@ OPTIONS seules les sockets ouvertes sont affichées. -n, --numeric Afficher uniquement les adresses IP au lieu du nom d'hôte. - -z, --zport - Ne pas exiger que le port soit spécifié" + -z, --allow-no-port + Ne pas exiger que le port soit spécifié + --show none,ip,port,state + Spécifier d'afficher comme informations supplémentaire: rien, l'adresse + ip, le port et/ou l'état. Par défaut, afficher le port et l'état." } function filter_proto() { case "$1" in tcp) awk '$1 == "tcp" { print }';; tcp6) awk '$1 == "tcp6" { print }';; - *) cat;; + *) awk 'NR > 2 { print }';; esac } @@ -40,15 +43,27 @@ function filter_port() { } function print_conn() { - awk '{ client = $5; sub(/:[^:]+$/, "", client); print client " " $6 }' + awkrun show_ip:int="$1" show_port:int="$2" show_state:int="$3" '{ + match($5, /:[^:]+$/) + client = substr($5, 1, RSTART - 1) + port = substr($5, RSTART + 1) + state = $6 + + line = client + if (show_ip) line = line " " client + if (show_port) line = line " " port + if (show_state) line = line " " state + + print line +}' } function print_host() { local -a hosts if [ -n "$*" ]; then - while read count ip state; do + while read count ip suppl; do resolv_hosts hosts "$ip" - echo "$count" "${hosts:-$ip}" "$state" + echo "$count ${hosts:-$ip} $suppl" done else cat @@ -58,23 +73,47 @@ function print_host() { proto= all= resolve=1 -zport= +allow_no_port= +suppls=() parse_opts "${PRETTYOPTS[@]}" \ --help '$exit_with display_help' \ -4,--only-tcp proto=tcp \ -6,--only-tcp6 proto=tcp6 \ -a,--all all=1 \ -n,--numeric resolve= \ - -z,--zport zport=1 \ + -z,--allow-no-port allow_no_port=1 \ + --show: suppls \ @ args -- "$@" && set -- "${args[@]}" || die "$args" port="$1" -[ -n "$port" -o -n "$zport" ] || die "Vous devez spécifier le port ou utiliser -z" +[ -n "$port" -o -n "$allow_no_port" ] || die "Vous devez spécifier le port ou utiliser -z" +# déterminer les informations supplémentaires à afficher +show_ip= +show_port= +show_state= +array_fix_paths suppls , +anysuppl= +for suppl in "${suppls[@]}"; do + case "$suppl" in + none|n) ;; + ip|i) show_ip=1;; + port|p) show_port=1;; + state|s) show_state=1;; + *) ewarn "$suppl: valeur non reconnue";; + esac + anysuppl=1 +done +[ -n "$anysuppl" ] || { + show_port=1 + show_state=1 +} + +# afficher les connexions LANG=C netstat -tn ${all:+-a} | filter_proto "$proto" | filter_port "$port" | -print_conn | +print_conn "$show_ip" "$show_port" "$show_state" | csort | "$scriptdir/umatch" -c -F "" | print_host $resolve