我正在使用以下代码:
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")
当谈到 stdout.read() 时,它挂起……有时它会在很长时间后打印输出。
你能建议一下这个问题是否可以做些什么吗??
我看到此问题已报告于:
https://bugs.python.org/issue24026
python 中是否有更好的模块用于 ssh 连接和运行命令?
原文由 user3378508 发布,翻译遵循 CC BY-SA 4.0 许可协议
可能与 https://github.com/paramiko/paramiko/issues/109 有关
以下是对我所面临的问题以及我如何解决它的解释。
我也遇到过这个问题,这是由于 stdout.channel.eof_received == 0
stdin、stdout 和 stderr 保持打开状态…
所以没有收到 EOF…
通常我收到 True 并且只能使用 stdout.read(),但为了安全起见,我使用了这个解决方法(有效!):等待超时,强制执行 stdout.channel.close(),然后执行 stdout.read():
顺便说一句,我使用:
希望这可以帮助…