TRENDING TELLER
TRENDING TELLER

Reputation: 31

What are the exit codes of command PING?

I want to know which exit codes (or errorlevel numbers) are returned by command ping to the parent process like cmd.exe on using this command in a Windows batch file or bash on using ping in a bash shell script in the following use cases?

  1. When all packets are sent and received.
  2. When no packets are received.
  3. Request timed out.
  4. Other conditions

Upvotes: 3

Views: 2579

Answers (1)

SomethingDark
SomethingDark

Reputation: 14305

On Windows, ping errorlevel is 0 unless the target host does not exist, then it is 1. There are no other errorlevels.

If you're trying to determine if ping was successful, check the output for the string TTL.

REM Use the full path for system tools in case the user makes bad script-naming decisions
set "ping=%SystemRoot%\System32\ping.exe"
set "find=%SystemRoot%\System32\find.exe"

%ping% %url% | %find% "TTL" >nul
if "%errorlevel%"=="0" (
    echo Ping was successful
) else (
    echo Ping was not successful
)

On Linux hosts, ping has an errorlevel of 0 on success, 1 if the host is unreachable, 2 if the target host does not exist.

Upvotes: 5

Related Questions