批量Ping一段IP地址

时间:2022-08-13 14:52:03

 花了两个小时写了一个简单的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

本文出自 “风尾” 博客,请务必保留此出处http://tshue.blog.51cto.com/521083/667791