Get output of system ping without printing to the console

前端 未结 2 1682
温柔的废话
温柔的废话 2021-01-05 16:22

I want to call ping from Python and get the output. I tried the following:

response = os.system(\"ping \"+ \"- c\")

However,

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 16:34

    If you only need to check if the ping was successful, look at the status code; ping returns 2 for a failed ping, 0 for a success.

    I'd use subprocess.Popen() (and not subprocess.check_call() as that raises an exception when ping reports the host is down, complicating handling). Redirect stdout to a pipe so you can read it from Python:

    ipaddress = '198.252.206.140'  # guess who
    proc = subprocess.Popen(
        ['ping', '-c', '3', ipaddress],
        stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    if proc.returncode == 0:
        print('{} is UP'.format(ipaddress))
        print('ping output:')
        print(stdout.decode('ASCII'))
    

    You can switch to subprocess.DEVNULL* if you want to ignore the output; use proc.wait() to wait for ping to exit; you can add -q to have ping do less work, as it'll produce less output with that switch:

    proc = subprocess.Popen(
        ['ping', '-q', '-c', '3', ipaddress],
        stdout=subprocess.DEVNULL)
    proc.wait()
    if proc.returncode == 0:
        print('{} is UP'.format(ipaddress))
    

    In both cases, proc.returncode can tell you more about why the ping failed, depending on your ping implementation. See man ping for details. On OS X the manpage states:

    EXIT STATUS
         The ping utility exits with one of the following values:
    
         0       At least one response was heard from the specified host.
    
         2       The transmission was successful but no responses were received.
    
         any other value
                 An error occurred.  These values are defined in .
    

    and man sysexits lists further error codes.

    The latter form (ignoring the output) can be simplified by using subprocess.call(), which combines the proc.wait() with a proc.returncode return:

    status = subprocess.call(
        ['ping', '-q', '-c', '3', ipaddress],
        stdout=subprocess.DEVNULL)
    if status == 0:
        print('{} is UP'.format(ipaddress))
    

    * subprocess.DEVNULL is new in Python 3.3; use open(os.devnull, 'wb') in it's place in older Python versions, making use of the os.devnull value, e.g.:

    status = subprocess.call(
        ['ping', '-q', '-c', '3', ipaddress],
        stdout=open(os.devnull, 'wb'))
    

提交回复
热议问题