您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

详解linux查看TCP连接IP统计小程序代码

2024/7/1 8:14:09发布29次查看
这篇文章详解linux查看tcp连接ip统计小程序代码
#!/usr/bin/env python#coding:utf8import commandsimport timeimport sysdef main(count_list): print "数量\t\tip地址 \t\t\t连接状态" for i in count_list: for x in i.split(): if len(x) > 14: print "%s \t"% x, else: print "%s \t\t" % x, printif __name__ == '__main__': if len(sys.argv) >1: try: while 1: command_str = """netstat -tan | grep '""" + sys.argv[1] + """' |awk '{print $(nf-1),$nf}' | grep -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk -f [\ :] '{print $(nf-2),$nf}' | awk '{ip[$0]++}end{for ( i in ip) print ip[i],i | "sort -nr | head -n 20" }' 2>/dev/null """ count_list = commands.getstatusoutput(command_str)[1].split("\n") main(count_list) time.sleep(5) print print "-----------------------------------------------------" except: pass else: try: while 1: command_str = """netstat -tan|awk '{print $(nf-1),$nf}' | grep -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk -f [\ :] '{print $(nf-2),$nf}' | awk '{ip[$0]++}end{for ( i in ip) print ip[i],i | "sort -nr | head -n 20" }' 2>/dev/null """ count_list = commands.getstatusoutput(command_str)[1].split("\n") main(count_list) time.sleep(5) print print "-----------------------------------------------------" except: pass
执行结果
[root@duyunlong-100-70test]# tcpcheck 数量 ip地址 连接状态 313 172.16.6.11 established 189 172.16.6.13 established 176 172.16.6.12 established 64 172.16.100.50 established 50 172.16.100.51 established 43 172.16.3.20 established 31 172.16.1.64 established 31 172.16.1.62 established 31 172.16.1.61 established 30 172.16.4.20 established 30 172.16.3.81 established 30 172.16.3.80 established 30 172.16.3.66 established 30 172.16.3.21 established 30 172.16.1.65 established 30 172.16.1.63 established 30 172.16.1.60 established 30 172.16.1.103 established 30 172.16.1.102 established 30 172.16.1.101 established
或者可以跟参数
[root@duyunlong-100-70test]# tcpcheck time_wait 数量 ip地址 连接状态 15 172.16.3.21 time_wait 15 172.16.3.20 time_wait 2 172.16.6.24 time_wait
以上就是详解linux查看tcp连接ip统计小程序代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product