ssh.py 文件源码

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

项目:astoptool 作者: zouliuyun 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号