问题
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 foundping.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 find
to 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