6

When i ping servers with os.system in python i get multiple response codes. Command used - os.system("ping -q -c 30 -s SERVERANME")

  • 0 - Online
  • 256 - Offline
  • 512 - what does 512 mean ?
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
DevUser
  • 99
  • 1
  • 2
  • 5
  • The `-s` option of `ping` is used to set a packet size, but you have not specified one. You should either eliminate the `-s` or add a `packetsize`. – John Anderson Mar 06 '19 at 01:55

1 Answers1

10

Per the docs:

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

And the wait docs say:

Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.

So 0, 256 and 512 correspond to ping exiting normally (not killed by signal) with exit statuses of 0 == 0 << 8 (0 traditionally means "success"), 256 == 1 << 8 (1 typically means "normal" failure) and 512 == 2 << 8 (not consistent, but 2 is frequently used to indicate an argument parsing failure). In this case, you passed -s without providing the mandatory value (packetsize) that switch requires, so an exit status of 2 makes sense.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Are you sure about the return value of system? In Linux documentation they said: "If all system calls succeed, then the return value is the termination status of the child shell used to execute command." https://man7.org/linux/man-pages/man3/system.3.html – Sarcares Oct 11 '22 at 13:00
  • 1
    @Sarcares: Like I said, POSIX doesn't specify, so it ends up being system dependent. That said, if you read the next paragraph in that man page, it says "In the last two cases, the return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e., WIFEXITED(), WEXITSTATUS(), and so on)."; it's not just a single value, it's multiple values munged together into a single return value. If I run `python3 -c 'import os; print(os.system("exit 3"))'` on my Linux Python 3 system, the return value is printed is `768` (aka `3 << 8`). – ShadowRanger Oct 11 '22 at 23:02