考慮下面的代碼: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