Clear the TCP connection process stuck in CLOSE_WAIT

netstat -nap |grep :8009|grep CLOSE_WAIT | awk '{print $7}'|awk -F"\/" '{print $1}' |awk '!a[$1]++'  |xargs kill

netstat -nap |grep :8849|grep CLOSE_WAIT | awk '{print $7}' |awk '!a[$1]++'|xargs kill -a

Use netstat to list all port information, including process information,

Filter out port 8009,

Filter out the state is CLOSE_WAIT,

The seventh column of the result is the process information, the format is 12568/httpd,

So remove the /httpd part, leaving only the process number,

Then remove the duplicate process number,

Finally, use xargs to bring in the kill command to kill the corresponding process.

The filter under Windows is

netstat -ano|findstr CLOSE_WAIT

findstris a Windows command for simple string matching and filtering on the command line. Depending on your needs, you can use other filtering tools or regular expressions to further process netstatthe output of the command.

Use a PowerShell script to automatically close CLOSE_WAITconnections in state:

  • Open PowerShell and run it with administrator privileges.

  • Execute the following script, which will filter out all CLOSE_WAITconnections in state and close them:

    $closeWaitConnections = netstat -ano | findstr "CLOSE_WAIT"
    $regexPattern = "(\d+\.\d+\.\d+\.\d+):(\d+)\s+(\d+\.\d+\.\d+\.\d+):(\d+)\s+CLOSE_WAIT\s+(\d+)"
    
    $matches = $closeWaitConnections | Select-String -Pattern $regexPattern | ForEach-Object {
        [regex]::Matches($_, $regexPattern) | ForEach-Object {
            $localAddress = $_.Groups[1].Value
            $localPort = $_.Groups[2].Value
            $remoteAddress = $_.Groups[3].Value
            $remotePort = $_.Groups[4].Value
            $pid = $_.Groups[5].Value
    
            $process = Get-Process -Id $pid -ErrorAction SilentlyContinue
            if ($process) {
                Write-Host "Closing connection: LocalAddress=$localAddress, LocalPort=$localPort, RemoteAddress=$remoteAddress, RemotePort=$remotePort, PID=$pid, ProcessName=$($process.ProcessName)"
                Stop-Process -Id $pid -Force
            } else {
                Write-Host "Process with PID=$pid not found. Skipping..."
            }
        }
    }
    

    • After executing the script, it will list all CLOSE_WAITconnections in state and attempt to shut down the associated processes. If the process shuts down successfully, the appropriate connection information is displayed.

    • Note that the script will attempt to Stop-Processshut down the associated process using the command, and use -Forcethe option to force a process shutdown. Before executing this script, make sure you understand the possible consequences of closing a connection, and make sure that a closed connection does not affect the proper functioning of your system or application.

    • If the script fails to close some connections, you may need to manually find and close the processes associated with those connections.

(1) LISTEN: First, the server needs to open a socket for listening, and the status is LISTEN. /* The socket is listening for incoming connections. Listen for connection requests from remote TCP ports*/ 

(2) SYN_SENT: The client calls connect for active open through the application program. So the client tcp sends a SYN to request to establish a connection. Then the state is set to SYN_SENT. /*The socket is actively attempting to establish a connection. When sending the connection Wait for a matching connection request after the request */ 

(3) SYN_RECV: The server should send ACK to confirm the SYN of the client, and at the same time send a SYN to the client. Then the state is set to SYN_RECV /* A connection request has been received from the network. After receiving and sending a connection request Wait for acknowledgment of connection request */ 

(4) ESTABLISHED: Represents an open connection, the two parties can or have already exchanged data. /* The socket has an established connection. Represents an open connection, data can be sent to the user*/ 

(5) FIN_WAIT1: Active close (active close) The terminal application calls close, so its TCP sends a FIN request to actively close the connection, and then enters the FIN_WAIT1 state./* The socket is closed, and the connection is shutting down. Waiting for the remote TCP A connection break request, or an acknowledgment of a previous connection break request */ 

(6) CLOSE_WAIT: Passive close (passive close) After TCP receives FIN, it sends ACK to respond to FIN request (its reception is also passed to the upper application as the end of file), and enters CLOSE_WAIT. /* The remote end has shut down, waiting for the socket to close. Waiting for the connection interruption request sent from the local user */ ( 
  
7) FIN_WAIT2: After the active shutdown terminal receives the ACK, it enters FIN-WAIT-2 ./* Connection is closed , and the socket is waiting for a shutdown from the remote end. Waiting for a connection interruption request from the remote TCP */ 

(8) LAST_ACK: After passively closing the terminal for a period of time, the application that receives the end-of-file will call CLOSE to close the connection. This causes its TCP to also send a FIN, waiting for the other party's ACK. It enters LAST-ACK. /* The remote end has shut down, and the socket is closed. Waiting for acknowledgment. Waiting for the original connection to the remote TCP to be interrupted Acknowledgment of request */ 

(9) TIME_WAIT: After receiving the FIN at the active shutdown end, TCP sends an ACK packet and enters the TIME-WAIT state. /* The socket is waiting after close to handle packets still in the network. Wait for enough time to ensure that the remote TCP receives an acknowledgment of the connection interruption request */

(10) CLOSING: Relatively rare./* Both sockets are shut down but we still don't have all our data sent. Waiting for the remote TCP to confirm the connection interruption*/ 

(11) CLOSED: The passively closed end enters the closed state after receiving the ACK packet. The connection is over./* The socket is not being used. There is no connection status*/ 

The formation of the TIME_WAIT state only occurs on the side that actively closes the connection. 

    After receiving the FIN request from the passive closing party, the active closing party changes its state from FIN_WAIT2 to TIME_WAIT after successfully sending an ACK to the other party, and must wait for twice the MSL (Maximum Segment Lifetime, MSL is a datagram The time that can exist in the internetwork) After the time, both parties can change the status to CLOSED to close the connection. At present, the time to maintain the TIME_WAIT state in RHEL is 60 seconds

Guess you like

Origin blog.csdn.net/qq_23080741/article/details/121223994