【发布时间】:2020-04-27 07:36:33
【问题描述】:
我正在尝试使用 Paramiko 创建一个解决方案,该解决方案允许在输出中获取匹配标准的第一行。我添加了 while 循环以等待输出可用(有时命令将在一小时内运行更多)。 目前我有:
- 连接跳转主机
- 调用 shell 和 ssh 到第二台主机
- 运行命令
- 等待(使用 while 循环)所需的输出可用。
- 另存为字符串。
但是在需要的输出可用后,我在中断 while 循环时遇到了问题。
这是我的代码。
import paramiko
from time import sleep
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('jumphost', username='User', password='Passw')
sleep(2)
channel = ssh.invoke_shell()
channel.send('ssh secondHost\n')
sleep(2)
channel.send('Command1\n')
#sleep(2)
buff=''
while channel.recv_ready():
while not buff.endswith('$ '):
resp = channel.recv(9999)
for line in resp.split('\n'):
if line.startswith('Line1'):
print(line)
buff+=line
break
break
print 'buff', buff
ssh.close()
【问题讨论】:
标签: python ssh paramiko ssh-tunnel