How to Free TCP/UDP Port on Windows Using netstat and taskkill?
- 时间:2020-10-12 15:39:01
- 分类:网络文摘
- 阅读:141 次
If a port has been pocessed by a process/application/program, you cannot listen to it. Usually, an exception of “Address in Use – cannot bind” will be thrown. That is, a port can only be used by one program at a time. If you want to free up the port, you have to figure out which program occupies it.
On windows, you can use command netstat -ano to list the process and the ports.
-a Displays all connections and listening ports.
-n Displays addresses and port numbers in numerical form.
-o Displays the owning process ID associated with each connection.
If you run it, the output looks like this:
# netstat -ano Active Connections Proto Local Address Foreign Address State PID TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1320 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:1801 0.0.0.0:0 LISTENING 5052 TCP 0.0.0.0:2103 0.0.0.0:0 LISTENING 5052 TCP 0.0.0.0:2105 0.0.0.0:0 LISTENING 5052 TCP 0.0.0.0:2107 0.0.0.0:0 LISTENING 5052 TCP 0.0.0.0:2869 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:2968 0.0.0.0:0 LISTENING 14048 TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING 11184 TCP 0.0.0.0:6646 0.0.0.0:0 LISTENING 85592 TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING 784 TCP 0.0.0.0:49665 0.0.0.0:0 LISTENING 1824 TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 1920 TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 3316 TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 4276 TCP 0.0.0.0:49671 0.0.0.0:0 LISTENING 1068 TCP 0.0.0.0:49688 0.0.0.0:0 LISTENING 1032 TCP 0.0.0.0:49707 0.0.0.0:0 LISTENING 5052 TCP 127.0.0.1:4300 0.0.0.0:0 LISTENING 15296 TCP 127.0.0.1:4301 0.0.0.0:0 LISTENING 15296 TCP 127.0.0.1:5354 0.0.0.0:0 LISTENING 4752 TCP 127.0.0.1:5354 127.0.0.1:49669 ESTABLISHED 4752 TCP 127.0.0.1:5354 127.0.0.1:49670 ESTABLISHED 4752 TCP 127.0.0.1:5939 0.0.0.0:0 LISTENING 23360 TCP 127.0.0.1:5939 127.0.0.1:55573 ESTABLISHED 23360 TCP 127.0.0.1:6463 0.0.0.0:0 LISTENING 20872 TCP 127.0.0.1:27015 0.0.0.0:0 LISTENING 4760 TCP 127.0.0.1:27015 127.0.0.1:49708 ESTABLISHED 4760 TCP 127.0.0.1:28317 0.0.0.0:0 LISTENING 5152 TCP 127.0.0.1:49669 127.0.0.1:5354 ESTABLISHED 4760
where we can filter by the grep command (which can be downloaded via GNU Utilities for windows) however, you can use the windows command findstr to achieve the same thing, for example,
# netstat -ano | findstr 49669 STDIN TCP 127.0.0.1:5354 127.0.0.1:49669 ESTABLISHED 4752 TCP 127.0.0.1:49669 127.0.0.1:5354 ESTABLISHED 4760
The last column is the Process ID, where you can now kill the process by using taskkill /f /PID pid where /f means force.
taskkill /f /PID 4752
Of course, we can write a batch script (cmd) that uses for or the GNU-awk to filter out the last column and pipe the commands so that two steps will be merged into one. Using the AWK can be better where you can filter out the second and third column and look for the port numbers as the grep may mistakenly match the PID or the IP address when given the port number string.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:炫动卡通直播-上海炫动卡通卫视直播「高清」 BTV卡酷少儿直播-北京卡酷动画卫视直播「高清」 嘉佳卡通卫视直播-嘉佳卡通直播在线观看「高清」 浙江少儿频道直播-浙江少儿在线直播观看「高清」 广州少儿频道直播-广州少儿在线直播观看「高清」 福建少儿频道直播-福建少儿在线直播观看「高清」 内蒙古少儿频道直播-内蒙古少儿在线直播观看「高清」 劲爆体育直播-劲爆体育在线直播观看「高清」 辽宁体育直播-辽宁体育在线直播观看「高清」 北京体育频道直播-北京体育在线直播观看「高清」
- 评论列表
-
- 添加评论