View the tcp network connection status of netstat + awk in CentOS

Source: Internet
Author: User

View the tcp network connection status of netstat + awk in CentOS

Run the following command:

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

The following result is displayed, and the numbers are different:

FIN_WAIT_1 286

FIN_WAIT_2 960

SYN_SENT 3

LAST_ACK 32

CLOSING 1

CLOSED 36

SYN_RCVD 144

TIME_WAIT 2520

ESTABLISHED 352 # almost equal to the number of concurrent connections

This command can classify and summarize the network connection status of the current system.

This statement is seen at Zhang banquet. It is said that it was obtained from Wang daoda, Technical Director of Sina interactive community business department. It is very good.

The return parameters are described as follows:

SYN_RECV indicates the number of requests waiting for processing;

ESTABLISHED indicates the normal data transmission status;

TIME_WAIT indicates the number of requests that have been processed and wait for the timeout to end.

------------------------------------------------------------------
Let's take a look at awk:
/^ Tcp/
Filters records starting with tcp to shield irrelevant records such as udp and socket.
State []
It is equivalent to defining an array named state.
NF
Indicates the number of fields in the record. As shown above, NF is equal to 6.
$ NF
Indicates the value of a field. For the record shown above, $ NF is $6, which indicates the value of the 6th fields, that is, TIME_WAIT.
State [$ NF]
Indicates the value of the array element. The record shown above indicates the number of connections in the state [TIME_WAIT] state.
++ State [$ NF]
Add one number. The record shown above is to add one to the number of connections in the state [TIME_WAIT] state.
END
Indicates the command to be executed in the last stage
For (key in state)
Traverse Arrays
Print key, "\ t", state [key]
Print the keys and values of the array, and use the \ t tab in the middle to beautify it.

Related Article

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.