我有一个创建连接的类.我可以在通道关闭之前连接并执行1命令.在另一个系统上,我可以执行多个命令,并且通道不会关闭.显然它是我尝试连接的系统的配置问题.
class connect:
newconnection = ''
def __init__(self,username,password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('somehost', username=username,password=password,port=2222,timeout=5)
except:
print "Count not connect"
sys.exit()
self.newconnection = ssh
def con(self):
return self.newconnection
然后我使用’ls’命令打印一些输出
sshconnection = connect('someuser','somepassword').con()
stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")
print stdout.readlines()
print stdout
stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")
print stdout.readlines()
print stdout
sshconnection.close()
sys.exit()
在第一个exec_command运行后,它将打印目录列表的预期输出.当我在第一个exec_command之后打印stdout时,它看起来像是关闭了通道
<paramiko.ChannelFile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0x2400f10L (cipher aes128-ctr, 128 bits) (active; 0 open channel(s))>>>
就像我在另一个系统上说的那样,我能够继续运行命令并且连接不会关闭.有没有办法让我保持开放?或者更好的方式,我可以看到它关闭的原因?
编辑:所以看起来每个SSHClient.exec_command只能运行1个命令…所以我决定使用get_transport().open_session()然后运行一个命令.第一个总是有效的.第二个总是失败,脚本只是挂起
最佳答案 在exec_command执行后只有paramiko,关闭通道并且ssh返回auth提示符.
似乎只有paramiko,尝试织物或其他工具是不可能的.