实现windows批处理ping和tracert的输出结果

两个需求,各10分,共20分:

1、ping几个IP地址,要求可以ping通的IP在D:/ping.txt中输出yes,不能ping通的IP在D:/ping.txt中输出no,txt中只需按照ping的顺序逐行输出yes和no即可,不需要输出被ping的IP地址。
例:ping三个地址10.0.0.1、172.16.0.1、192.168.0.1,其中10.0.0.1ping不通后两个可以ping通,则在txt中输出结果为:
0
1
1

2、tracert几个IP地址,要求将tracert结果中的每一跳的IP输出,中间用分号间隔,直到到达目的地;每一行显示一个tracert的输出结果,下一个tracert的结果显示在下一行。
例:tracert两个IP地址1.1.1.1和2.2.2.2输出的示例如下:
1.1.3.1;1.1.2.1;1.1.1.1;
1.1.3.1;1.1.2.1;2.2.2.2;

先建立一个记录ip的记事本,比如在d:\ip.txt,在里面输入ip:
10.0.0.1
172.16.0.1
192.168.0.1
关闭保存。然后打开d:\1.txt,输入
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('type ip.txt^|find /v ""') do (
set a=%%i
ping !a!>2.txt
type 2.txt |findstr /c:"(100% loss)"
if %errorlevel%==0 (echo 0>>ping.txt) else (echo 1>>ping.txt)
)
保存1.txt,退出,重命名为1.bat,这样只要运行1.bat,它自动在ip.txt中读取ip,然后ping看看能不能通,如果不通,也就是掉包率100% loss,就在2.txt中输入0,通就输入1.

tracrt的话,建立3.bat,在里面输入
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('type ip.txt^|find /v ""') do (
set a=%%i
tracert !a!>4.txt
)
关闭保存,这样tracert的资料全都在4.txt中了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-10-05
1
@echo off
set #ip1=10.0.0.1
set #ip2=172.16.0.1
set #ip3= 192.168.0.1
(for /f "tokens=2 delims==" %%i in ('set #ip') do ping %%i>nul&&echo.1||echo.0)>d:\ping.txt

2

@echo off
set #ip1=10.0.0.1
set #ip2=172.16.0.1
set #ip3= 192.168.0.1
(for /f "tokens=2 delims==" %%i in ('set #ip') do (
for /f "skip=3 tokens=8" %%j in ('tracert -d %%i') do set /p a=%%j<nul)
echo.)>d:\tracert.txt
第2个回答  推荐于2018-05-18
1、PING(假设需要ping 的IP在 IP.txt中)
@echo off
for /f %%i in (IP.txt) do (
(ping -n 1 %%i | find "TTL") && echo ^1>>ping.txt || echo ^0>>ping.txt)

2、TRACERT
@echo off&setlocal EnableDelayedExpansion
for /f %%i in (IP.txt) do (
for /f "tokens=8 delims= " %%a in ('tracert %%i^|find "ms"') do (set t=!t!;%%a)
set t=!t:~1!
echo !t!>>TTT.txt)本回答被网友采纳
相似回答