简体   繁体   English

在paramiko频道清理recv

[英]Clean recv at paramiko channel

I want to establish SSH session, run first block of command, then run second block of command, but i need to capture output only from the second block. 我想建立SSH会话,运行第一个命令块,然后运行第二个命令块,但是我只需要捕获第二个块的输出。 How can I clean recv buffer in paramiko in more clean and right way ? 如何以更清洁正确的方式清理paramiko中的recv缓冲区? Now I re-establish SSH session, but it is ugly and stupid in my opinion 现在我重新建立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

Thanks in advance! 提前致谢!

Resolved in comments: 解决的评论:

what about recv()ing all available data from the channel before send()ing the second block? 在第二个块send()之前,rev()从通道中获取所有可用数据怎​​么办? – whjm – whjm

yes, it workes thanks ! 是的,它可以工作,谢谢! – pnd – pnd

You can try below code to clear buffer before "x.send('sh users\\n')" 您可以尝试以下代码来清除“ x.send('sh users \\ n')”之前的缓冲区

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM