def connect(self):
sshClient = paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
tries = 0
sshtry = state.sshtry
while True:
try:
tries += 1
sshClient.connect(self.ip,self.port,self.user,timeout=15,key_filename=["/home/astd/.ssh/authorized_keys","/home/astd/.ssh/id_rsa"])
self.sshClient = sshClient
self.transport = sshClient.get_transport()
break
except paramiko.BadHostKeyException, e:
raise NetworkError("Host key for %s did not match pre-existing key! Server's key was changed recently, or possible man-in-the-middle attack." % ip, e)
except (
paramiko.AuthenticationException,
paramiko.PasswordRequiredException,
paramiko.SSHException
), e:
msg = str(e)
#if e.__class__ is paramiko.SSHException and msg == 'Error reading SSH protocol banner':
if e.__class__ is paramiko.SSHException and msg.startswith('Error reading SSH protocol banner'):
#print "WARNNING: ip:%s %s"%(self.ip,msg)
if tries < sshtry:
time.sleep(1)
continue
else:
raise Exception(e)
else:
raise Exception(e)
except Exception,e:
raise Exception(e)
评论列表
文章目录