I've been playing with the subprocess
module to iteratively send
each line in an input file to a process created by the following command.
ssh -t -A $host 'remote_command'
The remote_command
expects a line in its STDIN, does some processing on the
line and iterates the cycle until STDIN closes or reaches EOF.
To achieve this, what I'd been doing was:
process = subprocess.Popen("ssh -t -A $host 'remote_command'",
shell=True,
stdin=subprocess.PIPE)
for line in file('/tmp/foo'):
process.stdin.write(line)
process.stdin.flush()
process.stdin.close()
But what I discovered was that the above method is not robust enough, as it is
often the case that remote_command
finishes prematurely without processing the
entire content (though sometimes the same code does succeed without a problem).
The situation is the same when I employ another, albeit very simiar, approach:
process = subprocess.Popen("ssh -t -A $host 'remote_command'",
shell=True,
stdin=file('/tmp/foo'))
So the question is: How can I make sure that each line in an input file be sent, received, and processed until the end by the remote machine in Python?