Una volta il tool per analizzare le statistiche di rete era netstat, ma adesso è stato soppiantato da ss.
ss è un tool che fa parte del pacchetto iproute2 ed è presente di default su tutte le distro Gnu/Linux di ultima generazione. ss ha preso il posto di netstat come abbiamo detto, grazie alla sua velocità, semplicità di utilizzo e per un output più human like, per non parlare tutte le informazioni vengono direttamene reperite interrogando il kernel.
Andando ad interrogare tutte le funzioni di ss, vediamo subito che sono facilmente intuibili e facili da ricordare:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
Usage: ss [ OPTIONS ] ss [ OPTIONS ] [ FILTER ] -h, --help this message -V, --version output version information -n, --numeric don't resolve service names -r, --resolve resolve host names -a, --all display all sockets -l, --listening display listening sockets -o, --options show timer information -e, --extended show detailed socket information -m, --memory show socket memory usage -p, --processes show process using socket -T, --threads show thread using socket -i, --info show internal TCP information --tipcinfo show internal tipc socket information -s, --summary show socket usage summary --tos show tos and priority information --cgroup show cgroup information -b, --bpf show bpf filter socket information -E, --events continually display sockets as they are destroyed -Z, --context display task SELinux security contexts -z, --contexts display task and socket SELinux security contexts -N, --net switch to the specified network namespace name -4, --ipv4 display only IP version 4 sockets -6, --ipv6 display only IP version 6 sockets -0, --packet display PACKET sockets -t, --tcp display only TCP sockets -M, --mptcp display only MPTCP sockets -S, --sctp display only SCTP sockets -u, --udp display only UDP sockets -d, --dccp display only DCCP sockets -w, --raw display only RAW sockets -x, --unix display only Unix domain sockets --tipc display only TIPC sockets --vsock display only vsock sockets --xdp display only XDP sockets -f, --family=FAMILY display sockets of type FAMILY FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help} -K, --kill forcibly close sockets, display what was closed -H, --no-header Suppress header line -O, --oneline socket's data printed on a single line --inet-sockopt show various inet socket options -A, --query=QUERY, --socket=QUERY QUERY := {all|inet|tcp|mptcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|packet_raw|packet_dgram|netlink|dccp|sctp|vsock_stream|vsock_dgram|tipc|xdp}[,QUERY] -D, --diag=FILE Dump raw information about TCP sockets to FILE -F, --filter=FILE read filter information from FILE FILTER := [ state STATE-FILTER ] [ EXPRESSION ] STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES} TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing} connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} bucket := {syn-recv|time-wait} big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing} |
se proviamo a digitare ss senza passare nessun parametro notiamo che restituisce un elenco esaustivo di socket TCP nella stato “established“, quindi connessioni attive.
Quindi partiamo con l’analizzare quelle che potrebbero essere una serie di opzioni, filtri o espressioni regolari che potremmo usare con ss, per poter effettuare un analisi o debug al volo dei socket in uso:
Comando | Descrizione |
ss -a | Mostra tutti i socket e tutti gli stati |
ss -l | Mostra solo i socket nello stato listening |
ss -t | Mostra solo TCP socket |
ss -u | Mostra solo UDP socket |
ss -f unix | Mostra i socket in base al tipo, amessi {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help} |
ss -w | Mostra solo i RAW socket |
ss dst 192.168.0.1 | Mostra tutti i socket con ip di destinazione 192.168.0.1 |
ss src 192.168.0.2 | Mostra tutti i socket con ip sorgente 192.168.0.2 |
ss -4 | Mostra tutti i socket IP versione 4 |
ss -6 | Mostra tutti i socket IP versione 6 |
sudo ss -t -p | Mostra tutti i processi che usano socket TCP |
ss -tn | Mostra tutti i socket TCP senza risolvere i nomi |
ss -tr | Mostra tutti i socket TCP risolvendo i nomi |
ss -t state listening | Mostra tutti i socket TCP nello STATO LISTENING, sono ammessi questi stati {all,connected,synchronized,bucket,big} |
ss -tr state established '( dport = :22 or sport = :22 )' | Mostra tutti i socket TCP, risolvendo i nomi e nello stato “established” applicando un filtro sulla porta di destinazione o sorgente 22 (quindi il socket SSH) |
ss dst :https | Mostra tutte le connessione che come porta di destinazione usano la 443 (in questo caso viene usato il nome al posto del numero) |
Come avete potuto notare, è possibile concatenare più opzioni e filtri, permettendo quindi una gestione molto più facile e semplificata.
Niente ci vieta di utilizzare l’output di ss e andarlo a gestire tramite grep, oppure per i più nostalgici andare ad installare netstat, per via delle altre informazioni che era possibile reperire e che adesso sono state demandate al comando ip.
Per una completa lista delle opzioni utilizzabili con ss, vi consiglio di interrogare l’help oppure man.
Comments are closed.