Board logo

标题: [网络连接] 批处理如何筛选ping命令的返回结果? [打印本页]

作者: cndw    时间: 2012-12-26 16:40     标题: 批处理如何筛选ping命令的返回结果?

在多个ping命令的批处理中,想选出返回 “Reques time out' 和'Destination host unreachable'这类不能结果,并执行某些处理,或导出到文档,或暂停执行。而其他返回正常就不再记录。要怎么实现啊。




PS :ping命令怎么才能只ping 一次就结束,不要返回4个结果,太多了。
作者: BAT-VBS    时间: 2012-12-26 21:39

  1. ping -n 1 127.0.0.1 | findstr /c:"Reques time out" /c:"Destination host unreachable"
复制代码

作者: cndw    时间: 2013-1-16 17:03

回复 2# BAT-VBS


  不是很明白,我要实现的功能实际上就是我要用批处理,ping几十个内部的IP地址,想把ping的结果导出到一个文本里面。只要记录哪几个是正常的,哪几个是不正常的。
作者: BAT-VBS    时间: 2013-1-16 17:19

回复 3# cndw


    请把所有的需求更新到顶楼
作者: BAT-VBS    时间: 2013-1-16 17:34

  1. @echo off
  2. >Success.txt type nul
  3. >Fail.txt type nul
  4. for /f %%a in (ip.txt) do (
  5.     ping %%a && echo>>Success.txt %%a || echo>>Fail.txt %%a
  6. )
复制代码

作者: ccdevil    时间: 2013-1-16 17:38

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f  %%a in (1.txt) do (
  4.   ping /n 1 /w 500 %%a|findstr /i "Reply">nul
  5.   if !errorlevel! equ 0 (echo %%a通) else (echo %%a不通)
  6. )
  7. )>log.txt
复制代码
1.txt内输入你要PING的ip地址
格式为:
192.168.1.1
192.168.65.254
192.168.65.1




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2