Linux查看并发连接数

Linux查看并发连接数

指令

  netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

结果

[root@izbp10ebr6tsvo83iahgzdz ~]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

CLOSE_WAIT 3

ESTABLISHED 87

FIN_WAIT2 9

TIME_WAIT 25

 

语句解析

命令的核心是awk指令简单的说就,用awk以 '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'规则去解析netstat -n的结果

第一步:netstat -n,结果如下

[root@izbp10ebr6tsvo83iahgzdz ~]# netstat -n 

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 192.168.0.162:57616     100.100.30.25:80        ESTABLISHED

tcp        0      0 192.168.0.162:6379      120.26.55.92:55830      ESTABLISHED

tcp        0      0 192.168.0.162:55764     120.26.55.92:6379       ESTABLISHED

tcp        0      0 192.168.0.162:48556     47.111.164.6:9092       ESTABLISHED

tcp        0      0 192.168.0.162:55776     120.26.55.92:6379       ESTABLISHED

tcp        0      0 192.168.0.162:50172     47.111.219.84:3306      ESTABLISHED

tcp        0      0 192.168.0.162:55766     120.26.55.92:6379       ESTABLISHED

tcp        0      0 192.168.0.162:443       101.247.3.249:41142     ESTABLISHED

tcp        0      0 192.168.0.162:55792     120.26.55.92:6379       ESTABLISHED

tcp        0      0 192.168.0.162:6379      120.26.55.92:55832      ESTABLISHED

tcp        0      0 192.168.0.162:443       101.247.3.249:41082     FIN_WAIT2  

tcp        0      0 192.168.0.162:27017     119.61.6.133:53930      ESTABLISHED

tcp        0      0 192.168.0.162:34212     120.26.55.92:27017      ESTABLISHED

tcp        0      0 192.168.0.162:55830     120.26.55.92:6379       ESTABLISHED

tcp        0      0 192.168.0.162:443       49.98.141.94:23375      TIME_WAIT  

tcp        0      0 192.168.0.162:6379      120.26.55.92:55782      ESTABLISHED

 

这里特别留意下第一列Proto和最后一列state

第二步:awk的解析,规则是 '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}',awk是逐行解析;

  2-1- /^tcp/,正则匹配当前行数据是以tcp开头

  2-2- $NF,是指当前行的最后一个字段,比如ESTABLISHED

  2-3- S[$NF],以$NF为index,建立数组S

  2-4-  {++S[$NF]} ,每当找到一个$NF,就在给数组(下标是$NF)的值加1;

  2-5- END, 表示执行完'/^tcp/ {++S[$NF]} ,在执行后面指令

  2-6- {for(a in S) print a, S[a]},这是一个for循环语句,表示遍历数组S,同时打输出下标和值

  2-7- awk '...' 解析并输出结果

 

END

Linux查看并发连接数

 

上一篇:EventLoop的全新理解方式


下一篇:ab压力测试