批平的结果为CSV [英] Batch ping results to CSV

查看:297
本文介绍了批平的结果为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个脚本,可以让我ping通从一个文本文件和输出的计算机列表是否响应或不CSV文件。当使用一台机器测试命令,我收到正确的错误级别的响应,但是当我尝试使用FOR语句它的每一个结果被列为错误级别0。

I'm trying to create a script that will let me ping a list of computers from a text file and output whether they respond or not to a CSV file. When testing the commands using a single machine I receive the correct errorlevel responses, but when I try it using a FOR statement every result is listed as errorlevel 0.

for /f %%g in (computers.txt) do (
    ping -n 1 %%g | findstr "TTL"
    if errorlevel equ 0 (
        echo %%g,success >> results.csv
    ) else (
        echo %%g,fail >> results.csv
    )
)

我是什么做错了吗?我已经试过code以上,以及从<一个href=\"http://stackoverflow.com/questions/13831218/batch-ping-a-list-of-computer-names-and-write-the-results-to-file\">Batch平计算机名称的列表,并将结果写入到文件(不返回任何响应)。有没有更好的方式来实现的结果呢?任何帮助将大大AP preciated。

What am I doing wrong? I've tried the code above, as well as that from Batch ping a list of computer names and write the results to file (which doesn't return any responses). Is there a better way to achieve the result? Any help would be greatly appreciated.

解决方案

for /f %%g in (computers.txt) do (
    for /f "tokens=1" %%a in ('ping -n 1 %%g ^| findstr /i /c:"try" /c:"out" /c:"TTL"') do (
    if %%a EQU Ping (echo.%%g,Could not find host>>results.csv)
    if %%a EQU Destination (echo.%%g,Destination host unreachable>>results.csv)
    if %%a EQU Request (echo.%%g,Request timed out>>results.csv)
    if %%a EQU Reply (echo.%%g,Replied>>results.csv))
)

更新为应对G的回答说。谢谢摹!

Updated as noted in the response to G's answer. Thank you G!

推荐答案

你能看到,如果这对你的作品?

Can you see if this works for you?

for /f %%g in (computers.txt) do (
    for /f "tokens=1" %%a in ('ping -n 1 %%g ^| findstr /i /c:"out" /c:"TTL"') do (
    if %%a EQU Reply (echo.%%g,success>>results.csv) else (echo.%%g,Fail>>results.csv)      
))

样本 -

(我的家用电脑,拥有互联网接入)

Sample -
(I am on home PC and has internet access)

Computers.txt

Computers.txt

google.com
12.2.2.3
gmail.com
90.2.3.1
yahoo.com
6.6.6.6
10.23.123.24
stackoverflow.com

我results.csv

My results.csv

google.com,success
12.2.2.3,Fail
gmail.com,success
90.2.3.1,Fail
yahoo.com,success
6.6.6.6,Fail
10.23.123.24,Fail
stackoverflow.com,success

干杯,G

这篇关于批平的结果为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆