示例#1
0
文件: views.py 项目: 9233/spug
def valid_ssh(hostname,
              port,
              username,
              password=None,
              pkey=None,
              with_expect=True):
    try:
        private_key = AppSetting.get('private_key')
        public_key = AppSetting.get('public_key')
    except KeyError:
        private_key, public_key = SSH.generate_key()
        AppSetting.set('private_key', private_key, 'ssh private key')
        AppSetting.set('public_key', public_key, 'ssh public key')
    if password:
        _cli = SSH(hostname, port, username, password=str(password))
        _cli.add_public_key(public_key)
    if pkey:
        private_key = pkey
    try:
        cli = SSH(hostname, port, username, private_key)
        cli.ping()
    except BadAuthenticationType:
        if with_expect:
            raise TypeError('该主机不支持密钥认证,请参考官方文档,错误代码:E01')
        return False
    except AuthenticationException:
        if password and with_expect:
            raise ValueError('密钥认证失败,请参考官方文档,错误代码:E02')
        return False
    return True
示例#2
0
文件: views.py 项目: ydd171/public
def valid_ssh(hostname, port, username, password, with_expect=True):
    try:
        private_key = AppSetting.get('private_key')  #定义私钥公钥
        public_key = AppSetting.get('public_key')
    except KeyError:  #错误检查
        private_key, public_key = SSH.generate_key()
        AppSetting.set('private_key', private_key, 'ssh private key')
        AppSetting.set('public_key', public_key, 'ssh public key')
    cli = SSH(hostname, port, username, private_key)
    if password:
        _cli = SSH(hostname, port, username, password=str(password))
        code, out = _cli.exec_command('mkdir -p -m 700 ~/.ssh && \
                echo %r >> ~/.ssh/authorized_keys && \
                chmod 600 ~/.ssh/authorized_keys' % public_key)
        if code != 0:
            raise Exception(f'add public key error: {out!r}')
    try:
        cli.ping()
    except BadAuthenticationType:
        if with_expect:
            raise TypeError('该主机不支持密钥认证,请参考官方文档,错误代码:E01')
        return False
    except AuthenticationException:
        if password and with_expect:
            raise ValueError('密钥认证失败,请参考官方文档,错误代码:E02')
        return False
    return True
示例#3
0
 def get_ssh_key(cls):
     public_key = cls.get_default('public_key')
     private_key = cls.get_default('private_key')
     if not private_key or not public_key:
         private_key, public_key = SSH.generate_key()
         cls.set('private_key', private_key)
         cls.set('public_key', public_key)
     return private_key, public_key
示例#4
0
def valid_ssh(hostname, port, username, password):
    try:
        private_key = AppSetting.get('private_key')
        public_key = AppSetting.get('public_key')
    except KeyError:
        private_key, public_key = SSH.generate_key()
        AppSetting.set('private_key', private_key, 'ssh private key')
        AppSetting.set('public_key', public_key, 'ssh public key')
    if password:
        cli = SSH(hostname, port, username, password=str(password))
        code, out = cli.exec_command('mkdir -p -m 700 ~/.ssh && \
                echo %r >> ~/.ssh/authorized_keys && \
                chmod 600 ~/.ssh/authorized_keys' % public_key)
        if code != 0:
            raise Exception(f'add public key error: {out!r}')
    else:
        cli = SSH(hostname, port, username, private_key)

    try:
        cli.ping()
    except AuthenticationException:
        return False
    return True