[摘要]这篇文章详解linux查看TCP连接IP统计小程序代码#!/usr/bin/env python#coding:utf8import commandsimport timeimport sysdef...
这篇文章详解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