gpt4 book ai didi

Python Paramiko - 在退出之前等待传递的命令的更多输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:15 24 4
gpt4 key购买 nike

我有以下代码

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
privatekeyfile = 'PK_FILE_PATH'
username ="USERNAME"
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
client.connect(hostname= IP, username=username, pkey=mykey)
command = SERVER_COMMAND

stdin, stdout, stderr = client.exec_command(command)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
stdoutLines = stdout.readlines()
print stdoutLines

我正在执行的命令在服务器上运行大约需要 10 秒。它最初返回一些信息(用户配置文件和模块版本),然后运行一些代码来检查一些本地服务器资源的状态。

Paramiko 在收到初始 header 信息后正在关闭连接。我需要它来等待服务器端命令的完整输出返回。我试过实现 tintin's solution here, with the same result

有什么想法吗?

最佳答案

添加 get_pty=True这将等到命令执行完成。stdin,stdout,stderr = self.ssh.exec_command(command,get_pty=True)

关于Python Paramiko - 在退出之前等待传递的命令的更多输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50726991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com