Ping exit code and python

17,822

Solution 1

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

Solution 2

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.

Share:
17,822

Related videos on Youtube

Con7e
Author by

Con7e

Updated on September 18, 2022

Comments

  • Con7e
    Con7e over 1 year

    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?

    • Matteo
      Matteo almost 10 years
      Which system are you using? How do you read the exit value?
    • Matthew Champion
      Matthew Champion almost 10 years
      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.
    • Con7e
      Con7e almost 10 years
      Yes, I noticed it in my debugging sessions. I was tempted of using a regex to get the ping message...
    • mpy
      mpy almost 10 years
      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?
  • cslotty
    cslotty almost 5 years
    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.