【bat】bat脚本每隔N秒自动获取当前服务器的TIME_WAIT,ESTABLISHED,LISTENING等

 自动获取当前:

@echo off
::启用变量延迟 !variable!
SETLOCAL ENABLEDELAYEDEXPANSION
echo 监控日期:%date:~0,10%>>TCP.txt
::intervalTime间隔时间(秒) 
set intervalTime=3 
:start
::if循环300次,每次间隔intervalTime秒
set /a n+=1
if %n% leq 300 (

::每个批处理命令结果赋值给变量
for /F %%i in ('netstat -ano ^|find /i /c "TIME_WAIT"') do ( set tcp1=%%i)
for /F %%i in ('netstat -ano ^|find /i /c "ESTABLISHED"') do ( set tcp2=%%i)
for /F %%i in ('netstat -ano ^|find /i /c "LISTENING"') do ( set tcp3=%%i)
:: ^|为转义,将上面3个cmd的命令结果写入到txt文件中
echo Time:%time:~0,2%:%time:~3,2%:%time:~6,2%^|TCP_TIME_WAIT:!tcp1!^|TCP_ESTABLISHED:!tcp2!^|TCP_LISTENING:!tcp3! >> TCP.txt
::间隔时间
choice /t %intervalTime% /d y /n >nul
goto :start)

你可能感兴趣的:(bat,tcp,bat,batch)