linux获取TCP连接数
程序员文章站
2024-02-24 10:28:40
...
linux获取TCP连接数
方法一:
[[email protected] ~]$ ss -ant | awk 'NR>1 {a[$1]++} END {for (b in a) print b,a[b]}'
ESTAB 535
TIME-WAIT 80
LISTEN 13
方法二:
[[email protected] ~]$ netstat -an | awk '/^tcp/ {a[$NF]++} END {for (b in a) print b,a[b]}'
TIME_WAIT 91
SYN_SENT 7
ESTABLISHED 535
LISTEN 13
方法三:
[[email protected] ~]$ cat /proc/net/snmp
Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagrams InUnknownProtos InDiscards InDelivers OutRequests OutDiscards OutNoRoutes ReasmTimeout ReasmReqds ReasmOKs ReasmFails FragOKs FragFails FragCreates
Ip: 1 64 12503948767 0 0 0 0 0 12503948767 12822437575 0 0 0 0 0 0 0 0 0
Icmp: InMsgs InErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps
Icmp: 985733 389 3779 69 0 0 0 490950 490935 0 0 0 0 984433 0 2548 0 0 0 0 490935 490950 0 0 0 0
IcmpMsg: InType0 InType3 InType8 InType11 OutType0 OutType3 OutType8
IcmpMsg: 490935 3779 490950 69 490950 2548 490935
Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts
Tcp: 1 200 120000 -1 1585243925 55029885 290059 15370 535 12500488405 12818246605 731883 25103 3821760
Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors
Udp: 2546210 3436 0 2657997 0 0
UdpLite: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors
UdpLite: 0 0 0 0 0 0
注:通过CurrEstab获取tcp连接数
[[email protected] ~]$ cat /proc/net/snmp | grep Tcp | tail -1 | awk '{print $10}'
535
扩展:如何获取每秒新增的TCP连接呢?
通过cat /proc/net/snmp得到最近240秒内PassiveOpens的增量,除以240可以得到每秒的tcp连接增量。
详情参考我的另一篇文章:https://blog.csdn.net/m0_37814112/article/details/80774935
如果想知道cat /proc/net/snmp输出选项各个参数的意义,可以参考
http://perthcharles.github.io/2015/11/10/wiki-netstat-proc/