批处理监控某些端口的bat批处理

分类
代码库
更新时间
2018-09-27 16:36:07
@echo off
Setlocal enabledelayedexpansion

echo 显示Qampp端口进程详细信息:
echo,
echo PID	PORT	TYPES		Exe			WorkingSize
echo ------------------------------------------------------------------ 
for /f "tokens=2,4,5 delims= " %%a in ('Netstat -ano ^| find "LISTENING" ^| find "TCP"') do (
   call :get_prot _PROT,%%a,getip
   set /a isread=0	
   if !_PROT! NEQ 0 (
	 if !_PROT!==80 (	
		set /a isread +=1
	 )
	if !_PROT!==443 (
		set /a isread +=1
	 )
	if !_PROT!==3306 (
		set /a isread +=1
	)

	if !_PROT!==21 (
		set /a isread +=1
	)
	if !_PROT!==6379 (
		set /a isread +=1
	)
	if !_PROT!==5432 (
		set /a isread +=1
	)
	if !_PROT!==8080 (
		set /a isread +=1
	)
	if !_PROT!==8005 (
		set /a isread +=1
	)
	 if !isread!==1 (
		call :getpidinfo %%c,Caption,PID,WorkingSetSize
        call :showinfo !PID!,!_PROT!,%%b,!Caption!,!WorkingSetSize!
	 )
   )
)
pause
goto :EOF

:showinfo
echo %1	%2	%3	%4		%5
goto :EOF

:get_prot
for /f "tokens=1,2 delims=:" %%a in ("%2") do (
   set "%3=%%a"
   call :checkint RET %%b
)
set "%1=%RET%"
goto :EOF

:checkint
echo %2|findstr /be "^[0-9]*$" >nul 2>nul && set port=1 || set port=0
if %port% GTR 0 (
  set "%1=%2" 
) else (
  set "%1=0"
)
goto:eof

:getpidinfo
rem  "tokens=1,2 delims=="
for /f "tokens=1,2 delims==" %%a in ('"wmic path Win32_Process WHERE ProcessId=%1 get Name,WorkingSetSize /value"') do (
    if "%%a" == "Name" (
      set %%a="%%b"
    )
    if "%%a" == "WorkingSetSize" (
	   set /a k=%%b/1024,m=k/1024	
       set "%%a=!m!MB"
    )
)

set "%2=%Name%"
set "%3=%1"
set "%4=%WorkingSetSize%"
goto:eof


bat语法真的挺恶心的.

完全没有规律可言, 基本的字符处理完全没有, 替换个字符都要烦死人.

经过努力, 还是把bat写出来了, 大家试试效果. 保存为bat 运行.


分享工作室[粤ICP备16004795号-2] 腾讯云 / 腾讯AI / 南沙新区 / 信用 0.012