运行一个简单的连续ping并将结果输出到csv文件



我正试图在cmd窗口中的Windows 10机器上运行一个简单的连续ping,并将时间戳和响应时间输出到csv文件中的两列。我真的不知道该怎么做,因为我对这种类型的编码没有任何经验。感谢您的帮助。

这一切都可以用batch语言完成,这相当于您使用cmd的请求。我在下面编写了一个简单的脚本,它将使用ping命令和findstr响应时间设置为变量。

脚本是连续的,可以通过以下设置从脚本顶部的配置进行调整:

  • 要ping的地址
  • ping之间的速度
  • 保存为/更新的文件名

脚本保存文件的方式是使用>>将输出追加到文件命令。所有脚本内上下文线索都列在::中,如果需要,可以从脚本中删除。请随意编辑下面的脚本以满足您的需求。如果您不知道如何将此脚本转换为批处理,请复制&粘贴到记事本中,另存为.bat

@ECHO OFF
@SETLOCAL
::Set the address you wish to ping
SET adress=123.123.12.1
::Set the speed between pings (In seconds)
SET speed=5
::Set the file name to save as - Can include location ex (C:Users%username%Desktopfile.csv)
SET filename=TP.csv
GOTO :ping
:ping
::Ping and extract time= into variable
for /F "tokens=7 delims== " %%G in ('ping -4 -n 1 %adress%^|findstr /i "time="') do set ping=%%G
echo Current ping for %adress%: %ping%
::Set Time stamp
set curTimestamp=%date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%%time:~6,2%
::Create file.csv at desired location
echo %curTimestamp%, %ping% >> %filename%
::Wait for x second before next ping
PING localhost -n %speed% >NUL
goto :ping
@echo off
::Set the address you wish to ping
set/p host=host Address: 
set logfile=Log_%host%.log
echo Target Host = %host% >%logfile%
::for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 -l 32') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL 
GOTO Ping)

最新更新