Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过批处理将输出Ping到csv_Csv_Batch File_Ping - Fatal编程技术网

通过批处理将输出Ping到csv

通过批处理将输出Ping到csv,csv,batch-file,ping,Csv,Batch File,Ping,我从该网站获得了以下批处理文件的大部分内容,并根据需要对其进行了修改: @echo off set/p host=host Address: set logfile=Log_%host%.log set csfile=pings_%host%.csv echo Target Host = %host% >%logfile% netsh interface show interface >>%logfile% nslookup myip.opendns.com resolv

我从该网站获得了以下批处理文件的大部分内容,并根据需要对其进行了修改:

@echo off

set/p host=host Address: 
set logfile=Log_%host%.log
set csfile=pings_%host%.csv

echo Target Host = %host% >%logfile%
netsh interface show interface >>%logfile%
nslookup myip.opendns.com resolver1.opendns.com >>%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 ') 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>>%csfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)
.log文件的输出正是我想要的。csv文件输出如下所示:

02/16/2016 Tue, 8:02:03,Reply from 173.194.115.33: bytes=32 time=16ms TTL=56
Date,Time,IP,Bytes,Time,TTL
02/16/2016 Tue,8:02:03,173.194.115.33,32,16ms,56
02/16/2016 Tue,8:02:04,173.194.115.33,32,17ms,56
...
我希望至少看到这样的情况:

02/16/2016 Tue, 8:02:03,Reply from 173.194.115.33:, bytes=32, time=16ms, TTL=56
最佳情况下,我希望csv如下所示:

02/16/2016 Tue, 8:02:03,Reply from 173.194.115.33: bytes=32 time=16ms TTL=56
Date,Time,IP,Bytes,Time,TTL
02/16/2016 Tue,8:02:03,173.194.115.33,32,16ms,56
02/16/2016 Tue,8:02:04,173.194.115.33,32,17ms,56
...
如果可能的话,我想远离PowerShell

谢谢,
Joe

您可以使用
FOR
循环来只解析出您关心的值

FOR /F "usebackq tokens=1-10 delims==:< " %%A IN (`ping google.com -n 1`) DO (
    REM Only process the response line.
    IF "%%A"=="Reply" (
        ECHO %%C,%%E,%%G,%%I
    )
)
FOR/F“usebackq tokens=1-10 delims==:<”%%A在(`ping google.com-n1`)DO中(
REM只处理响应线。
如果“%%A”==“回复”(
回声%%C、%%E、%%G、%%I
)
)
此代码段将以下数据转换为CSV格式:

IP,字节,时间,TTL

样本输出:

74.125.138.138,32,11ms,43

您应该能够很容易地将其适应您的脚本。

@ECHO OFF
SETLOCAL
设置“destdir=U:\destdir”
设置“host=google.com”
设置“csfile=%destdir%\pings\uU%host%.csv”
设置“flagfile=%destdir%\flagfile.flg”
::确保flagfile存在
回声。>%flagfile%“
回显(日期、时间、IP、字节、时间、TTL>%csfile%)
:PING
如果不存在“%flagfile%”转到:EOF
对于/f“令牌=*skip=2”%%A in('ping%host%%n 1')do(
对于/f“tokens=1,4,6,8,10delims=:=”%%P IN(“%time:~0,2%%%A”)DO(
回显%date%,%%P:%time:~3,2%:%time:~6,2%,%%Q,%%R,%%S,%%T>>%csfile%
)
回显%date%%时间:~0,2%:%time:~3,2%:%time:~6,2%%%A
超时1>NUL
后藤坪)
后藤:EOF
由于您已经对
日志文件
处理进行了排序,这将生成csvfile。我已经设置了适合我的系统的文件名,并为方便起见ping了Google

这将处理生产线

“8来自173.194.115.33的回复:,字节=32,时间=16ms,TTL=56”

假设“reply from”(毫无疑问,如果需要,可以选通)使用分隔符:=和空格,那么只需输出日期数据,后跟第一个标记、带有标点的分钟和秒以及剩余所需标记的逗号分隔列表

我添加了一个在第一个条目上创建的flagfile。如果删除该flagfile,批处理将停止,因此不必通过CtrlC终止进程