一个用来统计当前linux连接某个端口的IP数目的语句
为了要分析连接到当前linux的某个端口,比如80的所有外来IP的数,并且要来个排序
和统计,可以这样:
netstat -tn 2>/dev/null | grep :80 |grep "ESTABLISHED"| awk '{print $5}' | cut -d: -f 4 | sort | uniq -c | sort -nr | head
下面注意来分析:
1)netstat -tn 2>/dev/null
参数t,只显示tcp连接 -n:只显示数字,则会显示入下的形式:
tcp 0 0 64.91.*.*:80 114.198.236.100:12763 TIME_WAIT
tcp 0 0 64.91.*.*:80 175.136.226.244:51950 TIME_WAIT
tcp 0 0 64.91.*.*:80 175.136.226.244:51951 TIME_WAIT
tcp 0 0 64.91.*.*:23 202.127.210.2:14517 TIME_WAIT
tcp 0 0 64.91.*.*:80 149.238.193.121:65268 TIME_WAIT
tcp 0 0 64.91.*.*:80 114.198.236.100:44088 ESTABLISHED
tcp 0 0 64.91.*.*:80 175.136.226.244:51952 TIME_WAIT
2)但是只要established的,则grep "ESTABLISHED"
3)然后再AWK一下:
::ffff: 114.198.236.100:12763
::ffff: 175.136.226.244:51950
::ffff: 175.136.226.244:51951
::ffff: 149.238.193.121:65268
::ffff: 114.198.236.100:44088
::ffff: 175.136.226.244:51952
4)然后注意用cut,提取内容,CUT的用法见:
http://www.cnblogs.com/dong008259/archive/2011/12/09/2282679.html
cut -d : -f 4
这里用:分隔符号,结果为:
114.198.236.100
175.136.226.244
175.136.226.244
149.238.193.121
114.198.236.100
175.136.226.244
5) 然后再排序,group一下,然后按连接次数最多的排在前面
,最后甚至可以head一下,完成了
3 175.136.226.244
2 114.198.236.100
1 149.238.193.121
甚至再wc -l一下,得出当前连接80端口的estabalished的外部IP的总数