Netstat and awk combine statistical TCP status

Source: Internet
Author: User

Netstat and awk combine statistical TCP status


When the system administrator wants to know the status of the network connection, it often uses the netstat command to count the current network connection status of the server, and the following command with Netstat and awk is a good way to count the current network state.


Netstat-n|awk '/^tcp/{++state[$NF]} END {for (key in) print key, "\ T", State[key]} '


You will get a result similar to the following, the exact number will be different:

Time_wait 441

Close_wait 66

Established 521


In other words, this command categorizes the current system's network connection status.


The following explains why this is written:


A simple pipe character is connected to the Netstat and awk commands.


-----------------------------------------


First Look at Netstat:


Netstat-n


Active Internet connections (w/o servers)

Proto recv-q send-q Local address Foreign address state

TCP 0 0 172.16.1.38:7192 172.16.1.38:8233 time_wait


When you actually execute this command, you may get thousands of records like the one above, but we'll take one of them enough.


-----------------------------------------


Let's take a look at awk:


/^tcp/

Filter out the beginning of the TCP record, blocking UDP, socket and other unrelated records.


State[]

Equivalent to defining an array named state


Nf

Indicates the number of recorded fields, as shown above, NF equals 6


$NF

Represents the value of a field, such as the record shown above, $NF that is $6, which represents the value of the 6th field, which is time_wait


state[$NF]

The number of connections representing the values of the array elements, such as the record shown above, which is the state[time_wait] state


++state[$NF]

To add a number to a record, as shown above, is to add a state[time_wait] state to the number of connections


END

Represents the command to be executed in the final phase


For (key in state)

Iterating through an array


Print key, "\ T", State[key]

Print the array of keys and values, in the middle with a \ t tab split, beautify a bit.


This article is from the "Learning Journey" blog, so be sure to keep this source http://blogtank.blog.51cto.com/10511324/1962580

Netstat and awk combine statistical TCP status

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.