awk -n|more
zhutianpeng@ztp-OptiPlex-780:~/Icpp/server$ netstat -n|more 激活Internet连接 (w/o 服务器) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 28 0 10.8.0.171:43313 91.189.92.10:443 CLOSE_WAIT tcp 0 0 10.8.0.171:37374 116.213.120.232:80 ESTABLISHED tcp 28 0 10.8.0.171:49282 91.189.92.23:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34473 91.189.92.11:443 CLOSE_WAIT tcp 28 0 10.8.0.171:49286 91.189.92.23:443 CLOSE_WAIT tcp 0 0 10.8.0.171:36005 115.182.41.173:80 ESTABLISHED tcp 28 0 10.8.0.171:43473 91.189.92.10:443 CLOSE_WAIT tcp 28 0 10.8.0.171:49284 91.189.92.23:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34313 91.189.92.24:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34315 91.189.92.24:443 CLOSE_WAIT tcp 28 0 10.8.0.171:49280 91.189.92.23:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34471 91.189.92.11:443 CLOSE_WAIT tcp 28 0 10.8.0.171:43310 91.189.92.10:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34317 91.189.92.24:443 CLOSE_WAIT tcp 28 0 10.8.0.171:34468 91.189.92.11:443 CLOSE_WAIT tcp 28 0 10.8.0.171:43311 91.189.92.10:443 CLOSE_WAIT tcp 0 0 10.8.0.171:24800 10.8.0.219:49201 ESTABLISHED tcp 28 0 10.8.0.171:34319 91.189.92.24:443 CLOSE_WAIT tcp6 1 0 ::1:42019 ::1:631 CLOSE_WAIT udp 0 0 127.0.0.1:54875 127.0.1.1:53 ESTABLISHED
提示:实际执行这条命令的时候,可能会得到上万条类似的记录,我们只取若干来举例。
awk数组获得连接数:
netstat -n|awk '/^tcp/{++S[$NF]}END{for(a in S)print a," "S[a]}'
参考:http://oldboy.blog.51cto.com/2561410/1184165