Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。

[root@localhost ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     52 192.168.220.129:ssh     192.168.220.1:11774     ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    7580     /run/systemd/notify
unix  5      [ ]         DGRAM                    7591     /run/systemd/journal/socket
unix  26     [ ]         DGRAM                    7593     /dev/log

从命令的执行结果上看,netstat分为了两部分:

一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。

另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto:连接使用的协议,RefCnt:连接到本套接口上的进程号,Types:套接口的类型,State:套接口当前的状态,Path:连接到套接口的其它进程使用的路径名。

常用参数

-a (all)显示所有选项,默认不显示LISTEN相关

-t (tcp)仅显示tcp相关选项

-u (udp)仅显示udp相关选项

-n 拒绝显示别名,能显示数字的全部转化成数字。

-l 仅列出有在 Listen (监听) 的服務状态

-p 显示建立相关链接的程序名

-r 显示路由信息,路由表

-e 显示扩展信息,例如uid等

-s 按各个协议进行统计

-c 每隔一个固定时间,执行该netstat命令。

提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

常用参数组合:

1.列出所有端口

[root@localhost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.122.1:domain    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0      0 localhos:x11-ssh-offset 0.0.0.0:*               LISTEN     
tcp        0     52 192.168.220.129:ssh     192.168.220.1:11774     ESTABLISHED
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
tcp6       0      0 localhos:x11-ssh-offset [::]:*                  LISTEN     
udp        0      0 192.168.122.1:domain    0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 0.0.0.0:55575           0.0.0.0:*                          
udp        0      0 0.0.0.0:13621           0.0.0.0:*                          
udp        0      0 0.0.0.0:48475           0.0.0.0:*                          
udp6       0      0 [::]:10347              [::]:*                             
udp6       0      0 [::]:13776              [::]:*             

(-at 列出所有tcp端口,-au列出所有udp端口)

2.列出LISTEN端口

[root@localhost ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.122.1:domain    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0      0 localhos:x11-ssh-offset 0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
tcp6       0      0 localhos:x11-ssh-offset [::]:*                  LISTEN     
udp        0      0 192.168.122.1:domain    0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 0.0.0.0:55575           0.0.0.0:*                          
udp        0      0 0.0.0.0:13621           0.0.0.0:*                          
udp        0      0 0.0.0.0:48475           0.0.0.0:*                          
udp6       0      0 [::]:10347              [::]:*                             
udp6       0      0 [::]:13776              [::]:*

(-lt 列出所有tcp的监听端口,-lu 列出所有udp的监听端口)

3.列出协议的统计信息

[root@localhost ~]# netstat -s
Ip:
    1878 total packets received
    0 forwarded
    0 incoming packets discarded
    1696 incoming packets delivered
    1351 requests sent out
    65 dropped because of missing route
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    16 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 16
...

4.显示PID和进程名称

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.220.129:ssh     192.168.220.1:11774     ESTABLISHED 3737/sshd: root@pts 

5.显示不支持的地址组

netstat: no support for `AF IPX' on this system.
netstat: no support for `AF AX25' on this system.
netstat: no support for `AF X25' on this system.
netstat: no support for `AF NETROM' on this system.
例:输出访问tcp 22端口的客户机的ip和其访问的次数,并且输出前十

第一步通过netstat -ant查看当前tcp信息

[root@localhost ~]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN     
tcp        0     52 192.168.220.129:22      192.168.220.1:11774     ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 ::1:6010                :::*                    LISTEN     

第二步过滤出访问tcp 22端口的客户机ip

[root@localhost ~]# netstat -ant | awk '$1=="tcp"&&/:22/{print $5}' 
0.0.0.0:*
192.168.220.1:11774

第三步过滤出输出结果的ip地址

[root@localhost ~]# netstat -ant | awk '$1=="tcp"&&/:22/{split($5,clients,":");print clients[1]}' 
0.0.0.0
192.168.220.1

第四步进行统计

[root@localhost ~]# netstat -ant | awk '$1=="tcp"&&/:22/{split($5,clients,":");IPS[clients[1]]++}END{for(i in IPS)print i,IPS[i]}' 
0.0.0.0 1
192.168.220.1 1

最终代码:
[root@localhost ~]# netstat -ant | awk '$1=="tcp"&&/:22/{split($5,clients,":");IPS[clients[1]]++}END{for(i in IPS) print i,IPS[i]}' | sort -nr |head -n 10