有人知道如何通过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).


当前回答

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

其他回答

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

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

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

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

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

完成了!

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

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