批量ping一个计算机名称列表并将结果写入文件 [英] Batch ping a list of computer names and write the results to file

查看:195
本文介绍了批量ping一个计算机名称列表并将结果写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码会将计算机名称和ip地址写入文件,但是我希望它也可以写出无法ping的计算机的名称,并在其旁边添加失败。我不知道如何修改批处理文件来做到这一点。

  @echo off 

Echo Pinging list ...

set ComputerList = list.txt

Echo Computername,IP Address> Final.csv
setlocal enabledelayedexpansion
$ b $ (%ComputerList%)do(
for / ftokens = 3%% B in('ping -n 1 -l 1 %% A ^ | findstr Reply')do(
set IPadd = %% B
echo %% A,!IPadd:〜0,-1!>> Results.csv
))

pause


解决方案



$ pre使用由 findstr 设置的errorlevel替换返回的字符串(s):

> ('ping -n 1 -l 1 %% A ^ | findstr回应^ | ^ | echo未找到失败:')

其中 || (由于对于 ^ )表示仅在前一个命令失败时才执行。

请注意, ping 消息是依赖于系统语言的(它们被翻译成语言的操作系统)所以回复作为成功指示器只适用于英文版本。

The code below will write the computer name and ip address to file, but I would like it to also write the name of the computers it cannot ping with a fail next to it. I have no idea how I would modify the batch file to do this.

@echo off

Echo Pinging list...

set ComputerList=list.txt

Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Results.csv
))

pause

解决方案

You could use errorlevel set by findstr to substitute return string(s) if 'Reply' is not found:

('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:')  

where || (escaped here because of for context with ^) means execute only if previous command failed.

As a side note, you should be aware that ping messages are system language dependent (they are translated to language of OS) so 'Reply' as success indicator works only for English versions.

这篇关于批量ping一个计算机名称列表并将结果写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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