paramiko python模块挂起在stdout.read()上
我正在使用以下代码:paramiko python模块挂起在stdout.read()上,python,python-3.x,paramiko,python-module,Python,Python 3.x,Paramiko,Python Module,我正在使用以下代码: 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, p
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(),它会挂起。。。有时它会在长时间后打印输出
你能建议一下是否可以对这个问题做些什么吗
我看到这一问题已在以下网站上报道:
python中是否有更好的ssh连接和运行命令模块???可能与
下面是我面临的问题以及我如何应对的解释
我也经历了这个问题,这是由于
接收到的stdout.channel.eof_==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和stderr继续营业
>>> 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))>>>
通常我会收到True,并且只能使用stdout.read(),但为了安全起见,我使用了以下解决方法(有效!):
等待超时,强制stdout.channel.close()然后强制stdout.read():
顺便说一句,我使用:
Python 2.6.6
paramiko (1.15.2)
希望这有帮助……当标准输出中没有数据或有一行没有eol时(即在sh脚本中的read语句中),通常会发生这种情况。尝试设置“get_pty=True”,然后只读取标准输出中的字节。为了避免无限循环,最好设置一个超时和一个睡眠,尽管有continue语句:
stdin, stdout, stderr = ssh.exec_command("your-command",get_pty=True)
stdout.flush()
nbytes = 0
while (len(stdout.channel.in_buffer)==0):
continue
nbytes=len(stdout.channel.in_buffer)
print(nbytes)
stdout.read(nbytes)
我碰巧遇到了这个问题。但我用“readline”代替“readlines”来解决这个问题 例如:
client = paramiko.SSHClient()
client.connect(addr, port, username, password)
stdin, stdout, stderr = client.exec_command(cmd)
while True:
print(stdout.readline())
if stdout.channel.exit_status_ready():
break
因此,它将立即打印每一行,不再挂起,而且exit_status_ready()将确保循环在stdout停止/退出时中断。对于仍在努力解决此问题的任何人:我可以通过运行
client.close()来修复此问题
在尝试读取stdout
或stderr
之前,如果您希望在命令执行时获得特定的输出,则可以删除data=stdout.read()并编写此命令
while True:
if "Ended" in str(stdout.readline()):
stdout.channel.close()
break
在我的例子中,我的命令将返回一些已“结束”的内容,因此我将检查它,如果是,则执行stdout.channel.close()这修复了我的问题。在您的情况下,“Ended”可以是您希望得到的任何东西在使用stdout之前尝试关闭stdin 环境:
蟒蛇3.8
帕拉米科-2.7.2
stdin,stdout,stderr=client.exec_命令(cmd)
stdin.close()
对于iter中的线路(stout.readline,“”):
打印(行,结束=“”)
您将解决方案放在了哪里?由于client.exec_command
从未返回,我和发布此问题的人client.exec_command()。我们的代码挂在stdout.read()
上,为每个命令创建一个新的客户机似乎都是如此unnecessary@dmbhatti对。不过,这是一个有效的解决办法。我不相信投票最多的答案对我们正在运行的paramiko版本有效。不过我不确定。已经有一段时间了。请指定您使用的python版本?您很可能在OP使用Python2时使用python3。
client = paramiko.SSHClient()
client.connect(addr, port, username, password)
stdin, stdout, stderr = client.exec_command(cmd)
while True:
print(stdout.readline())
if stdout.channel.exit_status_ready():
break
while True:
if "Ended" in str(stdout.readline()):
stdout.channel.close()
break