Controllare le connessioni con Netstat

In questo tutorial spiegherò alcune delle funzioni più usate del famoso tool di diagnostica della rete: Netstat.

Il software è preinstallato sia in Windows che nelle distribuzioni di Linux, per cui le funzioni saranno le stesse. Cambiano però i flag, per cui vi consiglio di controllare la guida della versione in uso nel sistema operativo che state usando.

Netstat -l

Restituisce la lista delle connessioni in ascolto:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost.localdom:9665 0.0.0.0:*               LISTEN     
tcp        0      0 localhost.locald:zoomcp 0.0.0.0:*               LISTEN     
tcp        0      0 localhost.localdoma:ipp 0.0.0.0:*               LISTEN     
tcp6       0      0 localhost6.localdom:ipp [::]:*                  LISTEN     
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:50828           0.0.0.0:*                          
udp        0      0 0.0.0.0:11411           0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 localhost.localdoma:323 0.0.0.0:*                          
udp6       0      0 [::]:mdns               [::]:*                             
udp6       0      0 [::]:20738              [::]:*                             
udp6       0      0 [::]:56601              [::]:*                             
udp6       0      0 localhost6.localdom:323 [::]:*                             
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7

È possibile aggiungere il flag t o u avendo quindi per -lt e -lu solo le connessioni in listening riguardanti rispettivamente le porte tcp o udp.

Netstat -a

Restituisce una lista di tutte le porte upd e tcp attive sul sistema:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost.localdom:9665 0.0.0.0:*               LISTEN     
tcp        0      0 localhost.locald:zoomcp 0.0.0.0:*               LISTEN     
tcp        0      0 localhost.localdoma:ipp 0.0.0.0:*               LISTEN     
tcp        0      0 BadWolf:43088           ns237.altervista.o:http ESTABLISHED
tcp        0      0 BadWolf:52562           edge-star-shv-03-:https ESTABLISHED
tcp        0      0 BadWolf:43084           ns237.altervista.o:http TIME_WAIT  
tcp        0      0 BadWolf:38418           ec2-54-149-175-17:https TIME_WAIT  
tcp        0      0 BadWolf:59788           149.5.0.71:http         ESTABLISHED
tcp6       0      0 localhost6.localdom:ipp [::]:*                  LISTEN

Netstat -at

Restituisce la lista delle sole connessioni TCP.

Netstat -au

Restituisce la lista delle sole connessioni UDP.

Netstat -an

La lista delle connessioni viene presentata in formato numerico anziché hostname:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:9665          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:9666          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 10.202.237.240:46300    31.13.92.14:443         ESTABLISHED
tcp        0      0 10.202.237.240:52562    173.252.88.66:443       ESTABLISHED
tcp        0      0 10.202.237.240:59788    149.5.0.71:80           ESTABLISHED
tcp6       0      0 ::1:631                 :::*                    LISTEN

Netstat -r

Restituisce informazioni sulla tabella di routing del kernel:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 wlp2s0
10.202.237.0    0.0.0.0         255.255.255.0   U         0 0          0 wlp2s0

Netstat -s

Visualizza una completa descrizione delle statistiche:

Ip:
    Forwarding: 2
    1650822 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    1650571 incoming packets delivered
    1060534 requests sent out
    8 outgoing packets dropped
Icmp:
    55 ICMP messages received
    4 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 55
    75 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 75

Netstat -i

Mostra la lista delle interfacce di rete:

Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
enp1s0    1500        0      0      0 0             0      0      0      0 BMU
lo       65536       40      0      0 0            40      0      0      0 LRU
wlp2s0    1500  1712412      0      0 0       1093104      0      0      0 BMRU

Netstat -c

Visualizza una lista costantemente aggiornata delle informazioni sulle connessioni. Per interrompere premete CTRL+C:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 BadWolf:59788           149.5.0.71:http         ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22274    /run/user/992/systemd/notify
unix  23     [ ]         DGRAM                    9222     /run/systemd/journal/dev-log
unix  7      [ ]         DGRAM                    9228     /run/systemd/journal/socket
unix  2      [ ]         DGRAM                    22903    /run/user/1000/systemd/notify
unix  2      [ ]         DGRAM                    2045     /run/systemd/notify
unix  3      [ ]         STREAM     CONNECTED     24527    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     23926    /run/dbus/system_bus_socket
unix  2      [ ]         DGRAM                    23912    
unix  3      [ ]         STREAM     CONNECTED     23586    /run/systemd/journal/stdout

Netstat -ap

Visualizza il PID di ogni processo e il nome del processo a cui ogni socket appartiene:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 localhost.localdom:9665 0.0.0.0:*               LISTEN      9821/java           
tcp        0      0 localhost.locald:zoomcp 0.0.0.0:*               LISTEN      9821/java           
tcp        0      0 localhost.localdoma:ipp 0.0.0.0:*               LISTEN      -                   
tcp        0      0 BadWolf:54612           ns336031.ip-188-16:http ESTABLISHED 9821/java           
tcp6       0      0 localhost6.localdom:ipp [::]:*                  LISTEN      -

Il tool ha molte altre funzioni altrettanto utili. Per esaminarle tutte è necessario consultare la guida.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.