Sujay
Sujay

Reputation: 9

Paramiko - Remote command execution

On execution some of the commands Paramiko returns fine. For a particular set command that runs on remote shell , the execution hangs and on killing the process i capture the following error.

Exception executing the tests
Traceback (most recent call last):
  File "cli_automation.py", line 333, in executeTests
    output = stdout.read()
  File "/usr/local/lib/python2.7/site-packages/paramiko-1.12.2-py2.7.egg/paramiko/file.py", line 134, in read
    new_data = self._read(self._DEFAULT_BUFSIZE)
  File "/usr/local/lib/python2.7/site-packages/paramiko-1.12.2-py2.7.egg/paramiko/channel.py", line 1260, in _read
    return self.channel.recv(size)
  File "/usr/local/lib/python2.7/site-packages/paramiko-1.12.2-py2.7.egg/paramiko/channel.py", line 617, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/usr/local/lib/python2.7/site-packages/paramiko-1.12.2-py2.7.egg/paramiko/buffered_pipe.py", line 137, in read
    self._cv.wait(timeout)
  File "/usr/local/lib/python2.7/threading.py", line 244, in wait
    waiter.acquire()
KeyboardInterrupt

Upvotes: 1

Views: 1461

Answers (1)

tintin
tintin

Reputation: 3356

see my answer to a different questions that contains a custom implementation for exec_command that avoids the common hang and empty response scenarios:

def myexec(cmd, timeout, want_exitcode=False):
  # one channel per command
  stdin, stdout, stderr = ssh.exec_command(cmd) 
  # get the shared channel for stdout/stderr/stdin
  channel = stdout.channel

  # we do not need stdin.
  stdin.close()                 
  # indicate that we're not going to write to that channel anymore
  channel.shutdown_write()      

  # read stdout/stderr in order to prevent read block hangs
  stdout_chunks = []
  stdout_chunks.append(stdout.channel.recv(len(c.in_buffer)))
  # chunked read to prevent stalls
  while not channel.closed or channel.recv_ready() or channel.recv_stderr_ready(): 
      # stop if channel was closed prematurely
      got_chunk = False
      readq, _, _ = select.select([stdout.channel], [], [], timeout)
      for c in readq:
          if c.recv_ready(): 
              stdout_chunks.append(stdout.channel.recv(len(c.in_buffer)))
              got_chunk = True
          if c.recv_stderr_ready(): 
              # make sure to read stderr to prevent stall    
              stderr.channel.recv_stderr(len(c.in_stderr_buffer))  
              got_chunk = True  
      '''
      1) make sure that there are at least 2 cycles with no data in the input buffers in order to not exit too early (i.e. cat on a >200k file).
      2) if no data arrived in the last loop, check if we already received the exit code
      3) check if input buffers are empty
      4) exit the loop
      '''
      if not got_chunk \
          and stdout.channel.exit_status_ready() \
          and not stderr.channel.recv_stderr_ready() \
          and not stdout.channel.recv_ready(): 
          # indicate that we're not going to read from this channel anymore
          stdout.channel.shutdown_read()  
          # close the channel
          stdout.channel.close()
          break    # exit as remote side is finished and our bufferes are empty

  # close all the pseudofiles
  stdout.close()
  stderr.close()

  if want_exitcode:
      # exit code is always ready at this point
      return (''.join(stdout_chunks), stdout.channel.recv_exit_status())
  return ''.join(stdout_chunks)

Upvotes: 1

Related Questions