可能与超时有关。尝试将timeout参数(以秒为单位)添加到调用:exec_command(timeout=20*60)。这是20分钟的例子。
有关详细信息,请参见该方法中的doc string:def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False):
"""
Execute a command on the SSH server. A new `.Channel` is opened and
the requested command is executed. The command's input and output
streams are returned as Python ``file``-like objects representing
stdin, stdout, and stderr.
:param str command: the command to execute
:param int bufsize:
interpreted the same way as by the built-in ``file()`` function in
Python
:param int timeout:
set command's channel timeout. See `Channel.settimeout`.settimeout
:return:
the stdin, stdout, and stderr of the executing command, as a
3-tuple
:raises SSHException: if the server fails to execute the command
"""
我也经历过这个问题接收到stdout.channel.eof_==0
^{pr2}$
stdin,stdout和stderr保持开放。。。在>>> print stdin
>>
>>> print stdout
>>
>>> print stderr
>>
所以没有收到EOF。。。在>>> print stdin.channel.eof_received
0
通常我收到的都是真的标准读(),但为了安全起见,我使用了这个解决方法(有效!):等待超时,强制stdout.channel.close()然后标准读():>>> timeout = 30
>>> import time
>>> endtime = time.time() + timeout
>>> while not stdout.channel.eof_received:
... sleep(1)
... if time.time() > endtime:
... stdout.channel.close()
... break
>>> stdout.read()
'Starting XXX: \n[ OK ]\rProgram started . . .\n'
>>>