有人知道如何通过windows命令行关闭单个连接的TCP或UDP套接字吗?

我在谷歌上搜索了一下,看到一些人也问了同样的问题。但是答案看起来像是netstat或netsh命令的手册页,重点关注如何监视端口。我不想要关于如何监控它们的答案(我已经这样做了)。我想干掉他们。

EDIT, for clarification: Let's say that my server listens TCP port 80. A client makes a connection and port 56789 is allocated for it. Then, I discover that this connection is undesired (e.g. this user is doing bad things, we asked them to stop but the connection didn't get dropped somewhere along the way). Normally, I would add a firewall to do the job, but this would take some time, and I was in an emergency situation. Killing the process that owns the connection is really a bad idea here because this would take down the server (all users would lose functionality when we just want to selectively and temporally drop this one connection).


为了关闭该端口,您可以确定正在侦听该端口的进程并杀死该进程。


如果不关闭拥有这些套接字的进程,就不能关闭套接字。套接字属于打开它们的进程。因此要找出Unix/Linux的进程ID (PID)。像这样使用netstat:

netstat -a -n -p -l

这将打印如下内容:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     PID/Program name   
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN     1879/sendmail: acce 
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN     1860/xinetd         

其中-a打印所有的套接字,-n显示端口号,-p显示PID, -l只显示正在监听的内容(这是可选的,取决于你想要什么)。

你想要的真实信息是PID。现在我们可以通过下面的操作关闭这个进程:

kill 1879

如果你要关闭一个服务,最好使用:

service sendmail stop

Kill实际上只是杀死了这个进程和它拥有的所有子进程。使用service命令运行init. conf中注册的关机脚本。d目录。如果你在一个服务上使用kill,它可能无法正常启动,因为你没有正确关闭它。这取决于服务。

不幸的是,Mac在这方面与Linux/Unix不同。你不能使用netstat。如果你对Mac感兴趣,请阅读本教程:

http://www.tech-recipes.com/rx/227/find-out-which-process-is-holding-which-socket-open/

如果你在Windows上使用TaskManager来杀死进程,使用services UI来关闭服务。你可以像Linux/Unix一样在Windows上使用netstat来识别PID。

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/netstat.mspx?mfr=true


如果没有拥有这些套接字,就不能关闭服务器上的套接字,因此如果没有在拥有服务器套接字的进程中运行代码,就不能实际关闭套接字。

然而,还有另一个选项告诉客户端关闭它的套接字。向客户端正在连接的端口发送RST TCP包将导致客户端断开连接。你可以使用nmap进行RST扫描。

http://nmap.org/


尝试Sysinternals/Microsoft的工具TCPView (GUI)和Tcpvcon(命令行)。 https://learn.microsoft.com/en-us/sysinternals/downloads/tcpview


是的,这是可能的。你不必是当前拥有套接字的进程来关闭它。考虑一下,远程计算机、网卡、网线和操作系统都可能导致套接字关闭。

还要考虑Fiddler和Desktop VPN软件可以将自己插入到网络堆栈中,并显示您的所有流量或重新路由所有流量。

所以你真正需要的是Windows提供一个API,直接允许这样做,或者有人写了一个程序,操作起来有点像VPN或Fiddler,并给你一种方法来关闭通过它的套接字。

至少有一个程序(CurrPorts)可以做到这一点,我今天用它来关闭在CurrPorts启动之前启动的进程上的特定套接字。当然,要做到这一点,您必须以管理员身份运行它。

Note that it is probably not easily possible to cause a program to not listen on a port (well, it is possible but that capability is referred to as a firewall...), but I don't think that was being asked here. I believe the question is "how do I selectively close one active connection (socket) to the port my program is listening on?". The wording of the question is a bit off because a port number for the undesired inbound client connection is given and it was referred to as "port" but it's pretty clear that it was a reference to that one socket and not the listening port.


打开cmd 输入netstat -a -n -o 找到TCP [IP地址]:[端口号]....#[target_PID]# (UDP同上) (顺便说一句,杀死[target_PID]没有为我工作) CTRL+ALT+DELETE,选择“启动任务管理器” 单击“进程”选项卡 打开“PID”列,方法如下:查看>选择列>选中“PID”复选框 找到感兴趣的PID和“END PROCESS” 现在您可以在[IP地址]:[端口号]上重新运行服务器,没有任何问题


我找到了正确答案。尝试Sysinternals中的TCPView,现在由微软拥有。你可以在http://technet.microsoft.com/en-us/sysinternals/bb897437上找到它


即时/可行/部分答案:https://stackoverflow.com/a/20130959/2584794 与前面使用netstat -a -o -n的答案不同,在没有使用这些端口的应用程序名称的情况下,要查看非常长的列表


您可以使用sysinternal中的tcpview等程序。我想它可以帮助您监控和消除不必要的连接。


使用TCPView: http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx 或CurrPorts: https://www.nirsoft.net/utils/cports.html

或者,如果你不想使用外部软件(顺便说一下,这些工具不需要安装),你可以简单地先运行netstat命令(最好是netstat -b),然后设置本地安全策略来阻止用户机器的IP地址,这就是我一直在做的不需要的甚至未知的连接-这允许你做任何事情没有任何外部软件(一切都是Windows附带的)…


Wkillcx是一个可靠的Windows命令行工具,用于从前面没有提到的命令行删除TCP连接。不过,它有时确实会在连接大量的服务器上出现问题。我有时使用tcpview进行交互式kill,但wkillcx可以在脚本中使用。


使用CurrPorts(免费且无需安装):http://www.nirsoft.net/utils/cports.html

/close <本端地址> <本端端口> <对端地址> <对端端口>{进程名}

例子:

# Close all connections with remote port 80 and remote address 192.168.1.10: 
/close * * 192.168.1.10 80
# Close all connections with remote port 80 (for all remote addresses): 
/close * * * 80
# Close all connections to remote address 192.168.20.30: 
/close * * 192.168.20.30 *
# Close all connections with local port 80: 
/close * 80 * *
# Close all connections of Firefox with remote port 80: 
/close * * * 80 firefox.exe

它还有一个很好的GUI,具有搜索和筛选功能。

注意:这个答案是huntharo和JasonXA的答案和评论组合在一起,并进行了简化,以方便读者阅读。例子来自CurrPorts的网页。


例如,您希望释放端口8080 然后,按照这些命令执行。

 netstat -ano
 taskkill /f /im [PID of the port 8080 got from previous command]

完成了!


是的,有可能关闭TCP或UDP端口,在DOS中有一个命令

TASKKILL /f /pid 1234 

我希望这对你有用


如果你在Windows 8、Windows Server 2012或更高版本上运行,并安装了PowerShell v4以上版本,你可以使用下面的脚本。它会发现与端口相关的进程并终止它们(即终止进程及其连接;不仅仅是联系)。

Code

#which port do you want to kill
[int]$portOfInterest = 80

#fetch the process ids related to this port
[int[]]$processId = Get-NetTCPConnection -LocalPort $portOfInterest | 
    Select-Object -ExpandProperty OwningProcess -Unique | 
    Where-Object {$_ -gt 0} 

#kill those processes
Stop-Process -Id $processId 

文档:

get - netttcpconnection - PowerShell的NetStat等效程序 选择对象-从对象中拉回特定属性/删除重复项 Where-Object -根据某些条件筛选值 Stop-Process - PowerShell的TaskKill等效程序


如果你知道你想要释放的端口,你可以通过寻找特定的端口来排序netstat列表,就像这样:

netstat -ano | findstr :8080

然后pid会出现在右边,你可以用taskkill杀死它。

taskkill /pid 11704 /F

此外,你可能想看看这个问题,这是专门为localhost,但我认为它是相关的:


CurrPorts不适合我们,我们只能通过ssh访问服务器,所以也没有TCPView。为了不断开其他连接,我们也不能终止该进程。我们最终做的是在Windows的防火墙上阻止连接,但这还没有被建议。是的,这将阻止所有符合规则的连接,但在我们的情况下,只有一个连接(我们感兴趣的):

netsh advfirewall firewall add rule name="Conn hotfix" dir=out action=block protocol=T
CP remoteip=192.168.38.13

替换为您需要的IP,并根据需要添加其他规则。


如果你知道你想要删除的特定端口,只需以admin的身份打开命令提示符(在windows上),然后:

NPX kill-port 1900

以上1900是我箱子的端口号。当我想关闭React-Native开发工具(和Expo)正在运行的端口时,我经常使用这个方法。原因是即使关闭了开发人员窗口或停止了服务器,端口仍然在使用中。