Testing for the `ERRORLEVEL` set by `ping` is futile because in some situations it sets the `ERRORLEVEL` to **zero** (success) even when it has not received any valid replies from the remote host !!!
However, the method described below is solid, portable and works with:
1. Translations into all other languages of e.g.: "ms", "TTL", "Reply from", "loss", "Unreachable", "Received", etc..., including into Russian and into Chinese.
2. Pinging an IPv4 address.
3. Pinging an IPv6 address (no `TTL` field)
4. Situations in which the error _"General failure."_ occurs.
5. Pinging an address that returns _"Request timed out."_
6. Pinging an address that returns _"Destination host unreachable."_
7. Pinging a resolvable alphanumeric host.
8. Pinging an unresolvable alphanumeric host, which causes the _"Ping request could not find host UnresolvableHost..."_ error to be returned.
<!-- language: lang-dos -->
@echo off
for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% GEQ 3 (
echo SUCCESS.
) else (
echo FAILURE.
)
If you save the code above to a file named e.g. `pingtest.bat`, you can use it like this:
<!-- language: lang-dos -->
pingtest 192.168.1.1
This method takes advantage of the presence of equal signs in the strings `bytes=` which are not affected by translations to other languages and do not occur in unsuccessful ping/echo results.
Also, modifying the switch `-n 1` to e.g. `-n 6` allows you to get a qualitative result in the variable `%res%` reflecting the quality of the connection.
With the code below, you can normalize the result in the variable `%res%` so it becomes `0` upon failure ...and upon success - the number of successful pings:
<!-- language: lang-dos -->
@echo off
for /F %%A in ('ping %1 -n 6 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% GEQ 3 (
set /A "res=%res%-2"
) else (
set "res=0"
)
echo The number of successful pings is %res%
To pause your program until the host is pinged successfully, just call it in a loop like this:
<!-- language: lang-dos -->
@echo off
:loop
for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% LSS 3 goto :loop
Testing for the `ERRORLEVEL` set by `ping` is futile because in some situations it sets the `ERRORLEVEL` to **zero** (success) even when it has not received any valid replies from the remote host !!!
However, the method described below is solid, portable and works with:
1. Translations into all other languages of e.g.: "ms", "TTL", "Reply from", "loss", "Unreachable", "Received", etc..., including into Russian and into Chinese.
2. Pinging an IPv4 address.
3. Pinging an IPv6 address (no `TTL` field)
4. Situations in which the error _"General failure."_ occurs.
5. Pinging an address that returns _"Request timed out."_
6. Pinging an address that returns _"Destination host unreachable."_
7. Pinging a resolvable alphanumeric host.
8. Pinging an unresolvable alphanumeric host, which causes the _"Ping request could not find host UnresolvableHost..."_ error to be returned.
<!-- language: lang-dos -->
@echo off
for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% GEQ 3 (
echo SUCCESS.
) else (
echo FAILURE.
)
If you save the code above to a file named e.g. `pingtest.bat`, you can use it like this:
<!-- language: lang-dos -->
pingtest 192.168.1.1
This method takes advantage of the presence of equal signs in the strings `bytes=` which are not affected by translations to other languages and do not occur in unsuccessful ping/echo results.
Also, modifying the switch `-n 1` to e.g. `-n 6` allows you to get a qualitative result in the variable `%res%` reflecting the quality of the connection.
With the code below, you can normalize the result in the variable `%res%` so it becomes `0` upon failure ...and upon success - the number of successful pings:
<!-- language: lang-dos -->
@echo off
for /F %%A in ('ping %1 -n 6 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% GEQ 3 (
set /A "res=%res%-2"
) else (
set "res=0"
)
echo The number of successful pings is %res%
To pause your program until the host is pinged successfully, just call it in a loop like this:
<!-- language: lang-dos -->
@echo off
:loop
for /F %%A in ('ping %1 -n 1 ^| findstr /C:^= ^| find /c /v ""') do (set "res=%%A")
if %res% LSS 3 goto :loop