考虑下面的代码:Python类和SSH连接不能很好地工作
class sshConnection():
def getConnection(self,IP,USN,PSW):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP,username=USN, password=PSW)
channel = client.invoke_shell()
t = channel.makefile('wb')
stdout = channel.makefile('rb')
print t //The important lines
return t //The important lines
except:
return -1
myConnection=sshConnection().getConnection("xx.xx.xx.xx","su","123456")
print myConnection
结果:
<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=1000 -> <paramiko.Transport at 0xfcc990L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
<paramiko.ChannelFile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0xfcc930L (unconnected)>>>
这意味着:在类方法中,t
连接点连接,但回国后这个连接描述符,连接丢失。
这是为什么,我该如何使它工作?
谢谢!
谢谢,那是我不知道的。 – user2162550