bat判斷3306端口號是否被佔用

::#####get netstat by port#####
:getnetbyport
if not "%~1"=="" (
    set PORT=%~1
    for /f "tokens=1,* delims=," %%a in ("!PORT!") do (
        set myport=%%a
        set PORT=%%b
        ::PORT==8888
        for /f "delims=" %%z in ('netstat -a -n -o^|find /i ":!myport! "') do (
            set tLine=%%z
            set tLine=!tLine:%%=$!
            call :getNetInfo "!tLine!" tProto tLocalAdd tForeignAdd tState tPID
            set tLine=!tLine:$=%%!            
            ::echo !tLine!
            for /f "tokens=1 delims= " %%j in ('tasklist /nh /fi "PID eq !tPID!"') do (
				echo 3306端口已佔用[%%j]
				goto :eof
            )
        )
    )
)

echo 安裝mysql
call :installMysql



goto :eof


::#####FUNCTION#####
:getNetInfo
::將netstat -ano的某一行分隔成不一樣的變量
::call :getNetInfo "<netstat output line>" tProto tLocalAdd tForeignAdd tState tPID
if not "%~1"=="" (
    for /f "tokens=1,2,3,4,5 delims= " %%i in ("%~1") do (
        set %2=%%i
        set %3=%%j
        set %4=%%k
        if "%%i"=="TCP" (
            set %5=%%l
            set %6=%%m
        ) else (
            set %5=
            set %6=%%l
        )
    )
)
goto :eof
相關文章
相關標籤/搜索