73 lines
1.7 KiB
Plaintext
73 lines
1.7 KiB
Plaintext
|
#!/bin/bash
|
||
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||
|
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
||
|
urequire DEFAULTS
|
||
|
|
||
|
function display_help() {
|
||
|
uecho "$scriptname: afficher les connexions TCP entrantes sur un port
|
||
|
|
||
|
USAGE
|
||
|
$scriptname [options] port
|
||
|
|
||
|
OPTIONS
|
||
|
-4, --only-tcp
|
||
|
-6, --only-tcp6
|
||
|
Se limiter au protocole spécifié. Par défaut, afficher toutes les
|
||
|
connexions, qu'elles soient en IPv4 ou en IPv6
|
||
|
-a, --all
|
||
|
Afficher tous les sockets, y compris les ports d'écoute. Par défaut,
|
||
|
seules les sockets ouvertes sont affichées.
|
||
|
-n, --numeric
|
||
|
Afficher uniquement les adresses IP au lieu du nom d'hôte."
|
||
|
}
|
||
|
|
||
|
function filter_proto() {
|
||
|
case "$1" in
|
||
|
tcp) awk '$1 == "tcp" { print }';;
|
||
|
tcp6) awk '$1 == "tcp6" { print }';;
|
||
|
*) cat;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
function filter_port() {
|
||
|
awkrun port="$1" '$4 ~ (".*:" port "$") { print }'
|
||
|
}
|
||
|
|
||
|
function print_conn() {
|
||
|
awk '{ client = $5; sub(/:[^:]+$/, "", client); print client " " $6 }'
|
||
|
}
|
||
|
|
||
|
function print_host() {
|
||
|
local -a hosts
|
||
|
if [ -n "$*" ]; then
|
||
|
while read ip state; do
|
||
|
resolv_hosts hosts "$ip"
|
||
|
echo "${hosts:-$ip}" "$state"
|
||
|
done
|
||
|
else
|
||
|
cat
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
proto=
|
||
|
all=
|
||
|
resolve=1
|
||
|
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= \
|
||
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
||
|
|
||
|
port="$1"
|
||
|
[ -n "$port" ] || die "Vous devez spécifier un port"
|
||
|
|
||
|
LANG=C netstat -tn ${all:+-a} |
|
||
|
filter_proto "$proto" |
|
||
|
filter_port "$port" |
|
||
|
print_conn |
|
||
|
csort -u |
|
||
|
print_host $resolve |
|
||
|
"$scriptdir/umatch" -c -F ""
|