1

I am using os.system ("ping 192.168.178.5") command to ping a machine. Funny thing is, I get exit code 0 even if ping fails.

How can I get something that tells me the ping failed/host is offline?

5
  • Which system are you using? How do you read the exit value?
    – Matteo
    Jul 15, 2014 at 12:20
  • Ciao Matteo. I am using Windows 7. To read the output I use something like "retcode = subprocess.call(command...)". I also tried reading from stdout, but it seems os.system doesn't print such a thing in a way I can get it. Also, os.system returns 0 if ITSELF finished good, regardless of the output from the command it executes. I am all hears for alternatives.
    – Con7e
    Jul 15, 2014 at 12:37
  • 2
    I just did a bit of testing and it seems, at least initially, that it will return 0 whenever there is a response from the target machine, but 1 otherwise. To explain, in the situation that there is no response at all (an example is in Windows it may say "Request timed out."), it will return 1, but if it gets a response, even if it is something like "Destination host unreachable.", it will still return 0. Try it out by pinging an address you know cannot be valid and seeing the response. This doesn't answer the question (hence it being a comment) but I thought that it would be useful to note. Jul 15, 2014 at 12:41
  • Yes, I noticed it in my debugging sessions. I was tempted of using a regex to get the ping message...
    – Con7e
    Jul 15, 2014 at 12:49
  • A DDG search "python ping" returns stackoverflow.com/q/316866/2037712 as first hit. There a a lot of possibilities discussed how to check if a host is up. In what way don't these answers suit your need?
    – mpy
    Jul 15, 2014 at 18:42

2 Answers 2

3
+50

I'm not a user of Python, but one can find a lot of material about this problem.

For example, the post Local network pinging in python contains two solutions that I reproduce here (not tested).

Solution 1 : Parsing the output of the ping command

import subprocess

hostname = "10.20.16.30"
output = subprocess.Popen(["ping.exe",hostname],stdout = subprocess.PIPE).communicate()[0]

print(output)

if ('unreachable' in output):
    print("Offline")

One can also test this way for "Request timed out".

Solution 2 : Using a socket (adapted from original code so not guaranteed)

s = socket(AF_INET, SOCK_STREAM)         # Creates socket
host = 'localhost' # Enter the IP of the workstation here 
port = 80                # Select port which should be pinged

try:
    s.connect((host, port))    # tries to connect to the host
except ConnectionRefusedError: # if failed to connect
    print("Server offline")    # server is offline

s.close()                      # close socket
1
  • Right - the socket method is what I have used! But it's not enough - you mentioned both, and you'll only be good if you actually use both! At least I found I had to ping the server first - and then try a socket connect. The problem is that the socket connect wouldn't return if the server wasn't valid / reachable at all.
    – cslotty
    Aug 8, 2019 at 10:16
3

The use of os.system ("ping 192.168.178.5") command to ping a machine depends from the system on which you are.

  • From an online help for Ping under windows:

    A successful PING does NOT always return an %errorlevel% of 0 Therefore to reliably detect a successful ping - pipe the output into FIND and look for the text "TTL"

    So it seems you should use instead of ping 192.168.178.5 something like ping -n 1 192.168.178.5 | find "TTL="

  • From man ping on Linux (System Manager's Manual: iputils)

    If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

    In this case you can use directly the exit code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .