angelcai1986 发表于 2011-9-28 15:20:55

批量Ping一段IP地址

  花了两个小时写了一个简单的Batch文件, 主要作用是Ping一段指定的IP并将结果保存到一个文本文件, 不要笑我, 水平有点差。 写的时候参考了这篇文章http://batcracker.blog.51cto.com/3576884/667421的内容, 功能还不完善, 没有什么防错措施, 还有只能使用同一网段的C类地址ping, 陆续会改进:
  @echo off
  echo *******************************************************************************
  echo * This Batch is used to automatically ping a series of continuous IP and save *
  echo * the result to a file.                                                       *
  echo *                                                                           *
  echo * Version: 1.0                                                                *
  echo * Author:Tom Xue                                                            *
  echo * Email:   tomfxue@gmail.com                                                *
  echo * Update:09/18/2011                                                         *
  echo *******************************************************************************
  echo.
  color 79
  echo Please input a start IP:
  set /p startIP=
  echo Please input a End IP:
  set /p endIP=
  echo Please input the full path of the Log file (D:\ping.txt):
  set logfile=d:\ping.txt
  set /p Logfile=
  date /t >%logfile%
  time /t 》%logfile%
  for /f "delims=. tokens=1,2,3" %%i in ("%startIP%") do set networkAdd=%%i.%%j.%%k
  for /f "delims=. tokens=4" %%i in ("%startIP%") do set startHostAdd=%%i
  for /f "delims=. tokens=4" %%i in ("%endIP%") do set endHostAdd=%%i
  :next
  if %startHostAdd% GTR %endHostAdd% goto finish
  echo Pinging %networkAdd%.%startHostAdd%
  ping -n 2 -w 2 %networkAdd%.%startHostAdd% >nul
  if errorlevel 1 goto fail
  if errorlevel 0 goto success
  :success
  echo successful!
  echo %networkAdd%.%startHostAdd% is reachable 》%logfile%
  goto counter
  :fail
  echo Fail!
  echo %networkAdd%.%startHostAdd% is NOT reachable! 》%logfile%
  goto counter
  :counter
  Set /a startHostAdd = StartHostAdd + 1
  goto next
  :finish
  echo Finished ping
  pause
本文由:SKF轴承 http://wwww.9-zc.com 整理发布
页: [1]
查看完整版本: 批量Ping一段IP地址