windows,每天运行 net stat 到 txt 文件,文件名中包含日期/时间

windows,每天运行 net stat 到 txt 文件,文件名中包含日期/时间

第一次发帖。

无法在此站点或其他站点上找到类似的解决方案。需要一个 CMD 批处理文件来运行“netstat -an”输出,每天将其输出到具有唯一文件名的文本文件中,以便前一天的内容不会被覆盖。感谢任何建议、帮助和指点!

答案1

第一的 :要获取日期和时间,您应该使用,WMIC因为它独立于操作系统区域设置、语言或用户选择的日期格式(控制面板/区域)。

@echo off
Title Get Date and Time using WMIC
Call :Get_Date_Time 
echo Date : %Year%-%Month%-%day%
echo Time : %Hour%:%Min%:%Sec%
Pause>nul & Exit
::********************************************************************************************
:Get_Date_Time
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set "MyDate=%%x"
set "Year=%MyDate:~0,4%"
set "Month=%MyDate:~4,2%"
set "Day=%MyDate:~6,2%"
set "Hour=%MyDate:~8,2%"
set "Min=%MyDate:~10,2%"
set "Sec=%MyDate:~12,2%
exit /b
::********************************************************************************************

连接列表

@echo off
Title Connections List by Hackoo 2021
Mode 90,42 & Color 0A
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM  --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO                    ***************************************
ECHO                      Running Admin shell... Please wait...
ECHO                    ***************************************

    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
setlocal EnableDelayedExpansion
Call :Get_Date_Time
Set "LogFile=%~dpn0_%Year%-%Month%-%day%.txt"
If exist "%LogFile%" Del "%LogFile%"

::Used to convert PID to process names.
for /f "tokens=1 delims=" %%A in ('tasklist') do call :PID %%A

echo [Program:PID]                  [LocalIP:Port]        [RemoteIP:Port]
(echo [Program:PID]                 [LocalIP:Port]        [RemoteIP:Port] & echo;)>"%LogFile%"

echo.
for /f "tokens=1-27 delims=: " %%A in ('netstat -ano') do call :netstat %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O %%P %%Q %%R %%S %%T %%U
If Exist "%LogFile%" start "log" "%LogFile%"
pause>nul
exit /b

:PID
   set pid_%2=%1
   exit /b

:netstat
set type=%1
set srcIP=%2
set srcPort=%3
set dstIP=%4
set dstPort=%5
set state=%6
set pid=%7
set name=!pid_%pid%!

::Filter local connections away.
if "%state%"=="" exit /b
if not "%type%"=="TCP" exit /b
if "%srcIP%"=="Local" exit /b
if "%dstIP%"=="*" exit /b
if "%srcIP%"=="%dstIP%" exit /b
if "%pid%"=="" exit /b

if "%dstPort%"=="[" (
   set state=LISTENING
   set srcPort=%dstIP%
   set pid=%9
   )
if "%dstPort%"=="[" set name=!pid_%pid%!

if "%name%"=="" set name=Unknown

::Formatting \tabs
set srcPortTab=     %srcPort%
set namePidTab=[%name%:%pid%]                                      
set srcIpPortTab=%srcIP%:%srcPort%                               
set dstIpPortTab=%dstIP%:%dstPort%                               
set stateTab=%state%                               
set namePidTab=%namePidTab:~0,32%
set srcIpPortTab=%srcIpPortTab:~0,21%
set dstIpPortTab=%dstIpPortTab:~0,21%
set stateTab=%stateTab:~0,12%
set srcPortTab=%srcPortTab:~-5%

if not "%state%"=="LISTENING" echo.%namePidTab% %srcIPPortTab% %dstIPPortTab% %stateTab%
if "%state%"=="LISTENING" echo.%namePidTab% Listening on: %srcPortTab%

(
    if not "%state%"=="LISTENING" echo.%namePidTab% %srcIPPortTab% %dstIPPortTab% %stateTab%
    if "%state%"=="LISTENING" echo.%namePidTab% Listening on: %srcPortTab%
)>>"%LogFile%"
exit /b
::********************************************************************************************
:Get_Date_Time
@for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set "MyDate=%%x"
set "Year=%MyDate:~0,4%"
set "Month=%MyDate:~4,2%"
set "Day=%MyDate:~6,2%"
set "Hour=%MyDate:~8,2%"
set "Min=%MyDate:~10,2%"
set "Sec=%MyDate:~12,2%
exit /b
::********************************************************************************************

答案2

我只需要这样做,我选择使用 PowerShell!它简单多了。

$datetime = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
Write-Host "Current date and time: $datetime"

Invoke-Expression "netstat -a" | Out-File -FilePath "netstat-$datetime.txt"
Write-Host "Netstat command executed successfully."

将文件命名为 netstat.ps1。要从任务计划程序调用它,请创建一个基本任务,然后powershell.exe在程序/脚本框中输入,然后在“添加参数框”中输入脚本的路径,例如:c:\ps\netstat.ps1。在“开始于”字段中输入c:\ps

相关内容