How to close TCP and UDP ports via windows command

2019-01-16 00:51发布

Does somebody knows how to close a TCP or UDP socket for a single connection via windows command line?

Googling about this, I saw some people asking the same thing. But the answers looked like a manual page of netstat or netsh commands focusing on how to monitor the ports. I don't want answers on how to monitor them (I already do this). I want to close/kill them.

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).

标签: networking
16条回答
Lonely孤独者°
2楼-- · 2019-01-16 01:18

Use CurrPorts (it's free and no-install): http://www.nirsoft.net/utils/cports.html

/close <Local Address> <Local Port> <Remote Address> <Remote Port> {Process Name}

Examples:

# 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

It also has a nice GUI with search and filter features.

Note: This answer is huntharo and JasonXA's answer and comment put together and simplified to make it easier for readers. Examples come from CurrPorts' web page.

查看更多
贪生不怕死
3楼-- · 2019-01-16 01:19

If you know the port that you want to free you can sort your netstat list by looking for the specif port like this:

netstat -ano | findstr :8080

Then the pid will appear at the rigth which you can kill with taskkill.

enter image description here

taskkill/pid 11704 /F

Also you may want to look at this question which is specifically for localhost, but I think it is relevant:

查看更多
做自己的国王
4楼-- · 2019-01-16 01:19

instant/feasible/partial answer : https://stackoverflow.com/a/20130959/2584794

unlike from the previous answer where netstat -a -o -n was used incredibly long list was to be looked into without the name of application using those ports

查看更多
劫难
5楼-- · 2019-01-16 01:21

Yes there is possible to close TCP or UDP port there is a command in DOS

TASKKILL /f /pid 1234 

I hope this will work for You

查看更多
登录 后发表回答