我正在编写一个脚本(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 连接并轻松接收命令输出的方法。
在此先感谢,美里。:)