【发布时间】:2021-03-23 14:10:58
【问题描述】:
我正在编写一个脚本(python 2.7),它与运行 Cisco IOS 的远程设备一起工作,所以我需要通过 ssh 执行很多命令。 很少有命令没有输出,其中一些有,我想接收输出。它是这样的:
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self._ip, port=22, username=username, password=password
stdin, stdout, stderr = ssh.exec_command('command with no output')
stdin, stdout, stderr = ssh.exec_command('command with no output')
stdin, stdout, stderr = ssh.exec_command('command with output')
sh_ver = stdout.readlines()
问题是exec_command 导致通道关闭并且无法重用,但我无法打开新通道来执行另一个命令,因为这是一个命令会话最后我需要得到输出。
我也尝试过以这种方式执行命令:
stdin, stdout, stderr = ssh.exec_command('''
command
command
command
''')
output = stdout.readlines()
但是这样,output 是空的。即使它不会,我也需要对output 执行一些检查,然后继续我停止的会话。
那么我需要什么?一种无需关闭或启动新连接即可管理此 ssh 连接并轻松接收命令输出的方法。
提前致谢,美里。 :)
【问题讨论】:
标签: python-2.7 ssh paramiko cisco cisco-ios