120 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/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.
 | 
						|
    -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 }';;
 | 
						|
    *) awk 'NR > 2 { print }';;
 | 
						|
    esac
 | 
						|
}
 | 
						|
 | 
						|
function filter_port() {
 | 
						|
    if [ -n "$1" ]; then
 | 
						|
        awkrun port="$1" '$4 ~ (".*:" port "$") { print }'
 | 
						|
    else
 | 
						|
        cat
 | 
						|
    fi
 | 
						|
}
 | 
						|
 | 
						|
function print_conn() {
 | 
						|
    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 suppl; do
 | 
						|
            resolv_hosts hosts "$ip"
 | 
						|
            echo "$count ${hosts:-$ip} $suppl"
 | 
						|
        done
 | 
						|
    else
 | 
						|
        cat
 | 
						|
    fi
 | 
						|
}
 | 
						|
 | 
						|
proto=
 | 
						|
all=
 | 
						|
resolve=1
 | 
						|
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,--allow-no-port allow_no_port=1 \
 | 
						|
    --show: suppls \
 | 
						|
    @ args -- "$@" && set -- "${args[@]}" || die "$args"
 | 
						|
 | 
						|
port="$1"
 | 
						|
[ -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 "$show_ip" "$show_port" "$show_state" |
 | 
						|
csort |
 | 
						|
"$scriptdir/umatch" -c -F "" |
 | 
						|
print_host $resolve
 |