类似于 netstat -np 但按状态和 PID 分组的命令?

Kin*_*ngo 10 linux rhel netstat

是否有类似于netstat -np但按状态PID分组的命令?

我想知道当前按程序分组的特定状态下的服务器连接数。

相似,

102 squid ESTABLISHED
32 httpd ESTABLISHED
Run Code Online (Sandbox Code Playgroud)

我使用RHEL5。

slm*_*slm 14

您可以使用您喜欢的任何格式sort重新组织输出netstat

$ netstat -anpt 2>&1 | tail -n +5 | sort -k7,7 -k 6,6
Run Code Online (Sandbox Code Playgroud)

这将首先使用第 7 列(进程名称/PID)然后是状态(ESTABLISHED、LISTEN 等)对输出进行排序。

注意:命令的第一部分netstat -anpt 2>&1 | tail -n +5 ..将 STDOUT 上可能出现的所有输出也定向到 STDIN,然后切掉前 5 行,这些行是netstat我们不感兴趣的样板输出。

例子

$ netstat -anpt 2>&1 | tail -n +5 | sort -k7,7 -k 6,6
tcp        0      0 192.168.1.20:49309      192.168.1.103:631       ESTABLISHED 2077/gnome-settings 
tcp        0      0 192.168.1.20:38393      204.62.14.135:443       ESTABLISHED 2260/mono           
tcp        0      0 192.168.1.20:39738      74.125.192.125:5222     ESTABLISHED 2264/pidgin         
tcp        0      0 192.168.1.20:40097      87.117.201.130:6667     ESTABLISHED 2264/pidgin         
tcp        0      0 192.168.1.20:53920      217.168.150.38:6667     ESTABLISHED 2264/pidgin         
...
tcp        1      0 192.168.1.20:50135      190.93.247.58:80        CLOSE_WAIT  24714/google-chrome 
tcp        1      0 192.168.1.20:44420      192.168.1.103:631       CLOSE_WAIT  24714/google-chrome 
tcp        0      0 192.168.1.20:36892      74.125.201.188:5228     ESTABLISHED 24714/google-chrome 
tcp        0      0 192.168.1.20:43778      74.125.192.125:5222     ESTABLISHED 24714/google-chrome 
tcp        0      0 192.168.1.20:33749      198.252.206.140:80      ESTABLISHED 24714/google-chrome 
...
Run Code Online (Sandbox Code Playgroud)

您可以使用类似的方法使用各种工具(例如wc或 )来获取计数uniq -c

改变输出

如果您真的想获得如下所示的输出netstat

102 squid ESTABLISHED
32 httpd ESTABLISHED
Run Code Online (Sandbox Code Playgroud)

您可以使用awk&进行一些进一步的切片和切块sed。这可以做得更紧凑,但应该让您开始并完成工作。

$ netstat -anpt 2>&1 | tail -n +5 | awk '{print $7,$6}' | sort -k1,1 -k3,3 \
    | sed 's#/# #' | column -t
2264   pidgin          ESTABLISHED
2264   pidgin          ESTABLISHED
24714  google-chrome   CLOSE_WAIT
24714  google-chrome   CLOSE_WAIT
24714  google-chrome   ESTABLISHED
24714  google-chrome   ESTABLISHED
...
24714  google-chrome   ESTABLISHED
26358  ssh             ESTABLISHED
26358  ssh             ESTABLISHED
26358  ssh             ESTABLISHED
26358  ssh             LISTEN
26358  ssh             LISTEN
26358  ssh             LISTEN
Run Code Online (Sandbox Code Playgroud)

注意: column -t只需将所有输出对齐到漂亮的列中。

计算连接数

最后在统计出现次数方面做你想做的事情:

$ netstat -anpt 2>&1 | tail -n +5 | awk '{print $7,$6}' | sort -k1,1 -k3,3 \
    | sed 's#/# #' | column -t | uniq -c
  6 -      LISTEN
  8 -      TIME_WAIT
  1 2077   gnome-settings  ESTABLISHED
  1 2260   mono            ESTABLISHED
 10 2264   pidgin          ESTABLISHED
  2 24714  google-chrome   CLOSE_WAIT
 27 24714  google-chrome   ESTABLISHED
  3 26358  ssh             ESTABLISHED
  4 26358  ssh             LISTEN
  1 26359  ssh             ESTABLISHED
  4 3042   thunderbird     ESTABLISHED
  1 32472  monodevelop     ESTABLISHED
  2 32472  monodevelop     LISTEN
  1 32533  mono            ESTABLISHED
  1 32533  mono            LISTEN
  1 3284   monodevelop     LISTEN
  1 3365   mono            LISTEN
  1 4528   mono            LISTEN
  1 8416   dropbox         ESTABLISHED
  1 8416   dropbox         LISTEN
Run Code Online (Sandbox Code Playgroud)

第一列代表计数。


mik*_*erv 5

来自维基百科

在 Linux 上,netstat(“net-tools”的一部分)已被弃用ss应改用(iproute2 的一部分)。

网络工具包还没有看到超过十年一个Linux发行版。对于旨在管理和监视不断发展的内核的通信接口的程序套件来说,如果没有更新,那将是很长一段时间 - 特别是当您谈论实际上运行互联网的内核时。

值得庆幸的是,有一个积极维护的iproute2包 - 其中包括该ss实用程序。

有了ss你,你可以做你要求的事情:

ss -np state ESTABLISHED
Run Code Online (Sandbox Code Playgroud)

来自man ss

#USAGE EXAMPLES
   ss -t -a
#          Display all TCP sockets.
#
   ss -t -a -Z
#          Display all TCP sockets with process SELinux
#          security contexts.
#
   ss -u -a
#          Display all UDP sockets.
#
   ss -o state established '( dport = :ssh or sport  =   :ssh )'
#          Display all established ssh connections.
#
   ss -x src /tmp/.X11-unix/*
#          Find  all  local  processes  connected  to X server.
#
   ss -o state fin-wait-1 '( sport = :http or sport  = :https )' dst 193.233.7/24
#          List all the tcp sockets in state FIN-WAIT-1
#          for our apache to network  193.233.7/24  and
#          look at their timers.
Run Code Online (Sandbox Code Playgroud)