View the TCP/IP statistics applet code in linux

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.