清理paramiko chann上的接收

2024-04-23 14:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立SSH会话,运行第一个命令块,然后运行第二个命令块,但我只需要从第二个块捕获输出。 我怎样才能以更干净和正确的方式清理paramiko中的recv buffer? 现在我重新建立了SSH会话,但在我看来,它既丑陋又愚蠢

client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect('10.10.10.50', username='admin', password='admin')
x=client.invoke_shell()
x.send('en')
x.send('\n')
x.send('conf t')
x.send('\n')
x.send('hostname 11')
x.send('\n')
x.send('exit')
x.send('\n')
x.close()
time.sleep(1)
client.connect('10.10.10.50', username='admin', password='admin')
x=client.invoke_shell()
x.send('sh users\n')
time.sleep(1)
output=x.recv(65535)
print output

提前谢谢!在


Tags: 命令clientsendparamikooutputadmintimeconnect
2条回答

您可以尝试下面的代码清除“x.send('sh users\n')”之前的缓冲区

while not x.recv_ready():
    time.sleep(10)
output = x.recv(65535).decode("utf-8")

在评论中解决:

what about recv()ing all available data from the channel before send()ing the second block? – whjm

yes, it workes thanks ! – pnd

相关问题 更多 >