paramiko python 模块挂在 stdout.read() [英] paramiko python module hangs at stdout.read()
问题描述
我正在使用以下代码:
import paramikodef runSshCmd(hostname, username, password, cmd, timeout=None):客户端 = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())客户端连接(主机名,用户名=用户名,密码=密码,allow_agent=False,look_for_keys=False,超时=超时)标准输入、标准输出、标准错误 = client.exec_command(cmd)标准输入.flush()数据 = stdout.read()打印(数据)客户端关闭()runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu")
当涉及到 stdout.read() 时,它挂起......有时它会在很长时间后打印输出.
能否请您建议是否可以对此问题采取任何措施??
我看到此问题已在以下位置报告:
https://bugs.python.org/issue24026
python 中是否有更好的模块用于 ssh 连接和运行命令??
可能与 https 相关://github.com/paramiko/paramiko/issues/109
以下是我面临的问题以及我如何解决它的解释.
我也遇到过这个问题,这是由于stdout.channel.eof_received == 0
import paramiko客户端 = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect("1.1.1.1", username="root", password="pass")stdin, stdout, stderr = client.exec_command("service XXX start")
stdin、stdout 和 stderr 保持打开状态...
<预><代码>>>>打印标准输入<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -><paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>>>>打印标准输出<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -><paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>>>>打印标准错误<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -><paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>所以没有收到EOF...
<预><代码>>>>打印 stdin.channel.eof_received0通常我会收到 True 并且只能使用 stdout.read(),但为了安全起见,我使用此解决方法(有效!):等待超时,强制 stdout.channel.close() 然后 stdout.read():
<预><代码>>>>超时 = 30>>>导入时间>>>结束时间 = time.time() + 超时>>>虽然不是 stdout.channel.eof_received:... 睡觉(1)...如果 time.time() >时间结束:... stdout.channel.close()... 休息>>>标准输出.read()'正在启动 XXX:\n[ OK ]\r程序已启动...\n'>>>顺便说一句,我使用:
Python 2.6.6paramiko (1.15.2)
希望这有帮助...
I am using the below code:
import paramiko
def runSshCmd(hostname, username, password, cmd, timeout=None):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password,
allow_agent=False, look_for_keys=False, timeout=timeout)
stdin, stdout, stderr = client.exec_command(cmd)
stdin.flush()
data = stdout.read()
print (data)
client.close()
runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu")
when it comes to stdout.read() , it hangs... sometimes it prints the output after long time.
Can you please suggest if anything can be done about this issue??
I see this issue has been reported in :
https://bugs.python.org/issue24026
Is there any better module in python for ssh connection and run commands ??
Could be related to https://github.com/paramiko/paramiko/issues/109
Below is explanation of what i am facing and how i worked around it.
I also experienced this issue it is due to stdout.channel.eof_received == 0
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("1.1.1.1", username="root", password="pass")
stdin, stdout, stderr = client.exec_command("service XXX start")
stdin, stdout and stderr are staying open...
>>> print stdin
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
>>> print stdout
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
>>> print stderr
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
So EOF was not received...
>>> print stdin.channel.eof_received
0
Usually I receive True and can just stdout.read(), but to be safe i use this workaround (which works!): Wait for a timeout, force stdout.channel.close() and then stdout.read():
>>> 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'
>>>
BTW i use:
Python 2.6.6
paramiko (1.15.2)
Hope this helps...
这篇关于paramiko python 模块挂在 stdout.read()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!