def __init__(self,ip,port=22,user="astd"):
self.ip = ip
self.port = port
self.user = user
sshClient = paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
tries = 0
sshtry = state.sshtry
while True:
try:
tries += 1
sshClient.connect(ip,port,user,timeout=5,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 tries < sshtry:
continue
else:
raise Exception(e)
else:
raise Exception(e)
except Exception,e:
raise Exception(e)
评论列表
文章目录