Paramiko - 远程命令执行
在执行时,Paramiko 有些命令运行得很好。但是对于某个特定的命令,它在远程终端上执行时会卡住。当我强制结束这个进程时,捕获到了以下错误信息。
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
1 个回答
1
请查看我在另一个问题中给出的答案,里面有一个自定义的exec_command实现,能够避免常见的卡住和空响应的情况:
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)