Test IP reachability with ERRORLEVEL of ping.exe

蹲街弑〆低调 提交于 2019-12-13 03:47:02

问题


Background

  • I want to check whether a specified host is IP reachable before some operation in .bat file.
  • I tried using ping.exe to test IP reachability, but I found ping.exe returns ERRORLEVEL 0 when the specified host is IP reachable or the network is unreachable.

Question

How can I test IP reachability of a specified host in .bat file?

Is there any way to distinguish IP reachable and network unreachable with ping.exe, or any other command to test IP reachability?


回答1:


You can use also use a structure similar to this, (I've used an Echo command upon success):

Ping -n 1 151.141.90.381 | Find "TTL=" >Nul && Echo host is reachable

…or more likely in your case, (this would end the script if the host was not reachable and any commands you would lke to run would follow that line):

Ping -n 1 151.141.90.381 | Find "TTL=" >Nul || GoTo :EOF



回答2:


Errorlevel of ping is not reliable (as you have noticed). (ab)use findto get an errorlevel

ping 192.168.0.99 | find "TTL="
echo %errorlevel%

(Note: searching for TTL= is the most reliable solution)



来源:https://stackoverflow.com/questions/47928636/test-ip-reachability-with-errorlevel-of-ping-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!