2014-09-16 18:56:02 +04:00
|
|
|
#!/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
|
2015-07-03 07:39:06 +04:00
|
|
|
Afficher uniquement les adresses IP au lieu du nom d'hôte.
|
|
|
|
-z, --zport
|
|
|
|
Ne pas exiger que le port soit spécifié"
|
2014-09-16 18:56:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function filter_proto() {
|
|
|
|
case "$1" in
|
|
|
|
tcp) awk '$1 == "tcp" { print }';;
|
|
|
|
tcp6) awk '$1 == "tcp6" { print }';;
|
|
|
|
*) cat;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
function filter_port() {
|
2015-07-03 07:39:06 +04:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
awkrun port="$1" '$4 ~ (".*:" port "$") { print }'
|
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
2014-09-16 18:56:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function print_conn() {
|
|
|
|
awk '{ client = $5; sub(/:[^:]+$/, "", client); print client " " $6 }'
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_host() {
|
|
|
|
local -a hosts
|
|
|
|
if [ -n "$*" ]; then
|
2014-09-17 14:20:01 +04:00
|
|
|
while read count ip state; do
|
2014-09-16 18:56:02 +04:00
|
|
|
resolv_hosts hosts "$ip"
|
2014-09-17 14:20:01 +04:00
|
|
|
echo "$count" "${hosts:-$ip}" "$state"
|
2014-09-16 18:56:02 +04:00
|
|
|
done
|
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
proto=
|
|
|
|
all=
|
|
|
|
resolve=1
|
2015-07-03 07:39:06 +04:00
|
|
|
zport=
|
2014-09-16 18:56:02 +04:00
|
|
|
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= \
|
2015-07-03 07:39:06 +04:00
|
|
|
-z,--zport zport=1 \
|
2014-09-16 18:56:02 +04:00
|
|
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
|
|
|
|
|
|
|
port="$1"
|
2015-07-03 07:39:06 +04:00
|
|
|
[ -n "$port" -o -n "$zport" ] || die "Vous devez spécifier le port ou utiliser -z"
|
2014-09-16 18:56:02 +04:00
|
|
|
|
|
|
|
LANG=C netstat -tn ${all:+-a} |
|
|
|
|
filter_proto "$proto" |
|
|
|
|
filter_port "$port" |
|
|
|
|
print_conn |
|
2014-09-17 14:20:01 +04:00
|
|
|
csort |
|
|
|
|
"$scriptdir/umatch" -c -F "" |
|
|
|
|
print_host $resolve
|