This article details linux viewing TCP/IP statistics applet code
#! /Usr/bin/env python # coding: utf8import commandsimport timeimport sysdef main (count_list ): print "quantity \ t \ tIP address \ t connection status" for I in count_list: for x in I. split (): if len (x)> 14: print "% s \ t" % x, else: print "% s \ 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 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {} '| 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 "-------------------------------------------------" failed t: 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] {} '| 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 "-------------------------------------------------------" failed t: pass
Execution result
[Root @ duyunlong-100-70test] # tcpcheck quantity IP address connection status 313 172.16.6.11 ESTABLISHED 189 172.16.6.13 255.176 255.255.255.64 255.255.50 255.255.43 255.255.31 255.255.31 172.16.1.62 ESTABLISHED 31 172.16.1.61 listen 30 172.16.3.81 ESTABLISHED 30 172.16.3.80 ESTABLISHED 30 172.16.3.66 255.30 172.16.3.21 255.30 255.255.30 255.30172.16.1.103 255.30 172.16.1.101 255.30 172.16.1.102 255.30 172.16.1.101
Or you can follow the parameters
[Root @ duyunlong-100-70test] # tcpcheck TIME_WAIT quantity IP address connection status 15 172.16.3.21 TIME_WAIT 15 172.16.3.20 TIME_WAIT 2 172.16.6.24 TIME_WAIT
The above is a detailed description of the linux TCP/IP statistics applet code details, please pay attention to the first PHP community other related articles!