Paramiko channel stucks when reading large ouput
i see no problem related to stdout channel, but i'm not sure about the way you are handling stderr. Can you confirm, its not the stderr capturing thats causing problem? I'll try out your code and let you know.
Update:
when a command you execute gives lots of messages in STDERR, your code freezes. I'm not sure why, but recv_stderr(600)
might be the reason.
So capture error stream the same way you capture standard output.
something like,
contents_err = StringIO.StringIO()
data_err = chan.recv_stderr(1024)
while data_err:
contents_err.write(data_err)
data_err = chan.recv_stderr(1024)
you may even first try and change recv_stderr(600)
to recv_stderr(1024)
or higher.
I am posting the final code which worked with inputs from Bruce Wayne( :) )
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(IPAddress, username=user['username'], password=user['password'])
chan = self.ssh.get_transport().open_session()
chan.settimeout(10800)
try:
# Execute the given command
chan.exec_command(cmd)
# To capture Data. Need to read the entire buffer to capture output
contents = StringIO.StringIO()
error = StringIO.StringIO()
while not chan.exit_status_ready():
if chan.recv_ready():
data = chan.recv(1024)
#print "Indside stdout"
while data:
contents.write(data)
data = chan.recv(1024)
if chan.recv_stderr_ready():
error_buff = chan.recv_stderr(1024)
while error_buff:
error.write(error_buff)
error_buff = chan.recv_stderr(1024)
exit_status = chan.recv_exit_status()
except socket.timeout:
raise socket.timeout
output = contents.getvalue()
error_value = error.getvalue()
return output, error_value, exit_status