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.