1

I am running a script remotely on a server via SSH and Python. This script looks through log files and returns some information based on each log entry it encounters. The problem I am running into is that although my script spits out the information as soon as it hits each log entry, my local machine needs to wait for the entire process to finish before it can read the lines from the ssh connection's stdout.

ssh = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE)
with open(remote_ip+'.hits', 'w') as f:
  for line in ssh.stdout:
    print line

Essentially this code prints all of the results all at once, at the end. I was wondering if there was a way for me to print out the contents of stdout as it was being produced on the server. Sorry if this is unclear, if something is ambiguous I will do my best to clarify it. Thanks!

EDIT: I should also add that I would preferably like to do this without any external packages, just built-in modules from 2.6 if possible.

xlnc
  • 47
  • 2
  • 8
  • Take a look: http://stackoverflow.com/questions/2082850/real-time-subprocess-popen-via-stdout-and-pipe – alecxe Aug 19 '13 at 20:07
  • Also [What is the simplest way to SSH using Python](http://stackoverflow.com/questions/1233655/what-is-the-simplest-way-to-ssh-using-python), and many other questions marked Related from both of the above. – abarnert Aug 19 '13 at 20:11
  • Replace `for line in ssh.stdout:` with `for line in iter(ssh.stdout.readline, ''):` due to [the read-ahead buffer Python bug](http://bugs.python.org/issue3907) – jfs Feb 19 '14 at 13:47

0 Answers0