如何用dat批处理文件关闭某端口对应程序-Windows自动化命令

如何用dat批处理文件关闭某端口对应程序?

网上找到的大部分都是手动操作,第一步先查出端口,第二步在根据上一步查到的端口手动去关闭进程。但我的需求不是这样的,我需要全自动处理。用于 dubbo 服务进程在 Windows 环境下的关闭。如果 Linux 环境,则不需那么辛苦了。

找了大半天,终于在百度知道上得到参考案例(或许是我使用的关键词不对),并成功试验,感谢相关贡献者。

答案一:

for /f "tokens=1-5 delims= " %%a in ('"netstat -ano|findstr "^:指定端口号""') do taskkill /pid %%e

 

答案二:

@echo off
set port=1234
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do taskkill /pid %%m

 

我自己试验成功的脚本如下:

@echo off
set port=20812
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
    echo kill the process %%m who use the port %port%
    taskkill /pid %%m
)

参考地址:http://zhidao.baidu.com/link?url=wGa-tP8nkQfi2bla1BRN5ZYjZ1TZzs3sr1QCPS0tS2NHbiUee9byFCNO31imk337DHvOanD05ezYL0NMF0Hopa

 

循环读取一批端口,并查询对应PID是否存在,若存在则关闭,脚本如下:

@echo off
for /l %%n in (20801,1,20812) do (
    @echo find the process which use port [%%n]
    for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%%n"') do (
        tasklist /FI "PID eq %%m"|find /i "PID" && (
        echo PID:%%m 运行中,kill the process [%%m] who use the port [%%n]
        taskkill /F /pid %%m
        ) || echo PID:%%m 未运行 
    )
)

以上脚本,参考资料如下:

http://blog.csdn.net/painss/article/details/4431580

http://3y.uu456.com/bp_1fudk0d4or4n7xz5ebbq_1.html

 

posted @ 2016-09-11 22:53  香巴拉  阅读(5448)  评论(0编辑  收藏  举报