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