环境: Python:2.7.6 帕拉米科:1.13.0 目标SSH服务器:Cisco MDS NX-OS 6.2(3)
代码:
import paramiko
import StringIO
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("switchname", username="username", password="password")
stdin, stdout, stderr = ssh.exec_command("term length 0")
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
t = stdout.read()
ssh.close()
for line in StringIO.StringIO(t):
line = line.replace("\n","")
print line
它只产生几行输出而不是整个输出。如果我修改上面的代码并添加一个ssh.exec_command,如下所示。
代码:
import paramiko
import StringIO
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("switchname", username="username", password="password")
stdin, stdout, stderr = ssh.exec_command("term length 0")
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
stdin2, stdout2, stderr2 = ssh.exec_command("show version ")
t = stdout.read()
ssh.close()
for line in StringIO.StringIO(t):
line = line.replace("\n","")
print line
产生以下错误:
Traceback (most recent call last):
File "paramiko_issue.py", line 15, in <module>
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
File "build/bdist.macosx-10.9-intel/egg/paramiko/client.py", line 307, in exec_command
File "build/bdist.macosx-10.9-intel/egg/paramiko/transport.py", line 543, in open_session
File "build/bdist.macosx-10.9-intel/egg/paramiko/transport.py", line 607, in open_channel
paramiko.ssh_exception.SSHException: SSH session not active
我非常感谢您解决此问题的任何帮助。提前谢谢。