Task: Try submitting ping to python using the most basic form, such as "ping 8.8.8.8". After some time, complete the ping command (in the terminal, one will do Ctrl + C) and get its output. Of particular interest are the last few lines of output, which show ping statistics.
Two methods tried, did not work. My OS version is Mac OS X 10.10.1.
The first method uses the pexpect module, and ping will stop after about 17 seconds, although I did not ask it to stop:
import pexpect import time child = pexpect.spawn('ping 8.8.8.8') (x, y) = child.getwinsize() print x print y time.sleep(21) child.terminate() x = child.read() print x
The second method uses the module subprocess, and the last few lines of ping output are lost:
import time from subprocess import PIPE, Popen child = Popen(['ping', '8.8.8.8'], stdin = PIPE, stdout = PIPE, stderr = PIPE) time.sleep(5) child.terminate() x = child.stdout.read() print x x = child.stderr.read() print x
I would be grateful for any help! "ping -c XXX" is not accepted.
python subprocess pexpect ping
brokenMotor Jan 17 '15 at 0:28 2015-01-17 00:28
source share