ssh.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:astoptool 作者: zouliuyun 项目源码 文件源码
def _connect(self,ip,port,user,sock=None):
        sshClient = paramiko.SSHClient()
        sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        tries = 0
        sshtry = state.sshtry
        sshTimeOut = state.sshTimeOut
        while True:
            try:
                tries += 1
                sshClient.connect(ip,int(port),user,timeout=sshTimeOut,sock=sock,key_filename=["/home/astd/.ssh/authorized_keys","/home/astd/.ssh/id_rsa"])
                sshClient = sshClient
                return sshClient
            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: reconnect ip:%s %s"%(self.ip,msg)
                    if tries < sshtry:
                        time.sleep(1)
                        continue
                    else:
                        raise Exception(e)
                else:
                    raise Exception(e)
            except Exception,e:
                if str(e) == "timed out" and tries < sshtry:
                    #print "Warnning %s:%s,retries ..."%(ip,str(e))
                    time.sleep(1)
                    continue
                raise e
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号