Rumah  >  Artikel  >  applet WeChat  >  详解linux查看TCP连接IP统计小程序代码

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

高洛峰
高洛峰asal
2017-03-24 13:08:312190semak imbas

这篇文章详解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

Atas ialah kandungan terperinci 详解linux查看TCP连接IP统计小程序代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn