awk统计TCP连接状态数量

  1. $ netstat -tnap
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1139/sshd
    tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2285/master
    tcp 0 96 192.168.2.17:22 192.168.2.1:2468 ESTABLISHED 87463/sshd: root@pt
    tcp 0 0 192.168.2017:22 192.168.201:5821 ESTABLISHED 89359/sshd: root@no
    tcp6 0 0 :::3306 :::* LISTEN 2289/mysqld
    tcp6 0 0 :::22 :::* LISTEN 1139/sshd
    tcp6 0 0 ::1:25 :::* LISTEN 2285/master

统计得到的结果:

  1. 5: LISTEN
    2: ESTABLISHED

awk统计TCP连接状态数量 - 图1

一行式:

  1. netstat -tna | awk '/^tcp/{arr[$6]++}END{for(state in arr){print arr[state] ": " state}}'
    netstat -tna | /usr/bin/grep 'tcp' | awk '{print $6}' | sort | uniq -c