O
O
Omich2013-11-25 16:55:43
Python
Omich, 2013-11-25 16:55:43

How to get the output of a ping command in Python?

To study python, I tried to write my own bike - a pinger of a certain host.
Actually, the ping command itself looks like this (I found it on some site):

result=subprocess.call(["ping",'-n','1', str(address)],shell=True,stdout=subprocess.PIPE)

As I understand it, stdout=subprocess.PIPE is the interception of the output of the command results.
So I want to parse this result and issue a warning not only for the presence / absence of a ping, but also for its duration.
How can I store the result of the ping command in a variable for further analysis?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2013-11-25
@kvothe

Use Popen. call() returns the exit code of the command.

A
alexzaporozhets, 2013-11-25
@alexzaporozhets

Try to use http://amoffat.github.io/sh/
>>> from sh import ping
>>> res = ping("-c", 1, 'yandex.ru')
>>> res
PING yandex.ru ( 93.158.134.11): 56 data bytes
64 bytes from 93.158.134.11: icmp_seq=0 ttl=57 time=34.563 ms
--- yandex.ru ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round- trip min/avg/max/stddev = 34.563/34.563/34.563/0.000 ms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question