给定以下代码:
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
连接是连接的,但是在返回这个连接描述符之后,连接就丢失了。
为什么会这样,我怎样才能让它工作?
谢谢!