Article title: Linux network connection analysis command. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Echo "Saving current connections ..."
# Netstat-nta>/tmp/netstat.txt
Echo "Number of connections per IP ..."
# Cut-B 49-75/tmp/netstat.txt | grep-o-P "\ B \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {} \ B "| sort | uniq-c | sort-n-r-k | head-10
Extract the number of connections for each IP address from the netstat output result, calculate the connection time for each connection, and output the 10 IP addresses with the maximum number of connections, it can be found that a single IP address has established too many connections to serve, and measures are taken to prevent this IP address.
Echo "States of connections ..."
# Cut-B 77-90/tmp/netstat.txt | sort | uniq-c
Extract the number of connections in different states from the netstat output results. To calculate the connection time in various states.
If a large number of connections exist under the SYN_RECV status, syn-flood attacks may occur.
Echo "Number of SYN_RECV connections per IP ..."
# Grep "SYN_RECV"/tmp/netstat.txt | cut-B 49-75 | cut-D ': '-f1 | sort | uniq-c | sort-n-r-k, 7 | head? 10
Use the output result of grep netstat to check the connections established by each IP address in the SYN_RECV state. in this way, the connection status of a single IP address is calculated and the 10 most connected SYN_RECV IP addresses are listed. If the preceding command has no output, the SYN_RECV status is normal.
Based on the commands listed above, you have analyzed the basic connection status to your server. However, this is not enough. Because you may not see many "short" connections being established. To analyze these new connections, you can do this:
Echo "Count number of new connection requests over the next 100 packets ..."
# Time tcpdump-ns 200-c 100 '(dst port http or dst port https) and tcp [13] & 2! = 0' | grep-o-p' \ B \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }\. \ d {1, 3 }. \ d {1, 5} \ s \> '| cut-D '. '-f 1-4 | sort | uniq-c | sort-n-r-k, 7 | head-25
In this command, parameter-c defines the number of data packets for analysis.