在paramiko频道清洁recv

时间:2017-05-10 21:05:51

标签: python ssh paramiko

我想建立SSH会话,运行第一个命令块,然后运行第二个命令块,但我需要仅从第二个块捕获输出。 如何以更清洁和正确的方式清理paramiko中的recv缓冲区? 现在我重新建立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

提前致谢!

2 个答案:

答案 0 :(得分:0)

已在评论中解决:

  

在send()第二个块之前recv()从通道获取所有可用数据怎​​么样? - whjm

     

是的,谢谢! - pnd

答案 1 :(得分:0)

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

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