Пример #1
0
def host_executor(q, host, pkey, command):
    exit_code, out, now = -1, None, time.time()
    try:
        cli = SSH(host.hostname, host.port, host.username, pkey=pkey)
        exit_code, out = cli.exec_command(command)
    finally:
        q.put((host.id, exit_code, round(time.time() - now, 3), out.decode() if out else None))
Пример #2
0
def host_executor(host, pkey, command):
    try:
        cli = SSH(host.hostname, host.port, host.username, pkey=pkey)
        exit_code, out = cli.exec_command(command)
        return exit_code == 0, out.decode()
    except Exception as e:
        return False, f'异常信息:{e}'
Пример #3
0
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
Пример #4
0
def host_executor(host, pkey, command):
    try:
        cli = SSH(host.hostname, host.port, host.username, pkey=pkey)
        exit_code, out = cli.exec_command(command)
        if exit_code == 0:
            return True, out or '检测状态正常'
        else:
            return False, out or f'退出状态码:{exit_code}'
    except Exception as e:
        return False, f'异常信息:{e}'
Пример #5
0
class Job:
    def __init__(self,
                 hostname,
                 port,
                 username,
                 pkey,
                 command,
                 token=None,
                 **kwargs):
        self.ssh_cli = SSH(hostname, port, username, pkey)
        self.key = f'{hostname}:{port}'
        self.command = command
        self.token = token
        self.rds_cli = None

    def _send(self, message, with_expire=False):
        if self.rds_cli is None:
            self.rds_cli = get_redis_connection()
        self.rds_cli.lpush(self.token, json.dumps(message))
        if with_expire:
            self.rds_cli.expire(self.token, 300)

    def send(self, data):
        message = {'key': self.key, 'type': 'info', 'data': data}
        self._send(message)

    def send_system(self, data):
        message = {'key': self.key, 'type': 'system', 'data': data}
        self._send(message)

    def send_error(self, data):
        message = {'key': self.key, 'type': 'error', 'data': data}
        self._send(message)

    def send_status(self, code):
        message = {'key': self.key, 'status': code}
        self._send(message, True)

    def run(self):
        if not self.token:
            return self.ssh_cli.exec_command(self.command)
        self.send_system('### Executing')
        code = -1
        try:
            for code, out in self.ssh_cli.exec_command_with_stream(
                    self.command):
                self.send(out)
        except socket.timeout:
            code = 130
            self.send_error('### Time out')
        except Exception as e:
            code = 131
            self.send_error(f'{e}')
        finally:
            self.send_status(code)
Пример #6
0
def host_executor(q, host, pkey, command):
    exit_code, out, now = -1, None, time.time()
    try:
        cli = SSH(host.hostname, host.port, host.username, pkey=pkey)
        exit_code, out = cli.exec_command(command)
        out = out.decode() if out else None
    except AuthenticationException:
        out = 'ssh authentication fail'
    except socket.error as e:
        out = f'network error {e}'
    finally:
        q.put((host.id, exit_code, round(time.time() - now, 3), out))
Пример #7
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
Пример #8
0
class Job:
    def __init__(self,
                 key,
                 name,
                 hostname,
                 port,
                 username,
                 pkey,
                 command,
                 interpreter,
                 token=None):
        self.ssh = SSH(hostname, port, username, pkey)
        self.key = key
        self.command = self._handle_command(command, interpreter)
        self.token = token
        self.rds_cli = None
        self.env = dict(SPUG_HOST_ID=str(self.key),
                        SPUG_HOST_NAME=name,
                        SPUG_HOST_HOSTNAME=hostname,
                        SPUG_SSH_PORT=str(port),
                        SPUG_SSH_USERNAME=username,
                        SPUG_INTERPRETER=interpreter)

    def _send(self, message, with_expire=False):
        if self.rds_cli is None:
            self.rds_cli = get_redis_connection()
        self.rds_cli.lpush(self.token, json.dumps(message))
        if with_expire:
            self.rds_cli.expire(self.token, 300)

    def _handle_command(self, command, interpreter):
        if interpreter == 'python':
            attach = 'INTERPRETER=python\ncommand -v python3 &> /dev/null && INTERPRETER=python3'
            return f'{attach}\n$INTERPRETER << EOF\n# -*- coding: UTF-8 -*-\n{command}\nEOF'
        return command

    def send(self, data):
        message = {'key': self.key, 'data': data}
        self._send(message)

    def send_status(self, code):
        message = {'key': self.key, 'status': code}
        self._send(message, True)

    def run(self):
        if not self.token:
            with self.ssh:
                return self.ssh.exec_command(self.command, self.env)
        self.send('\r\n\x1b[36m### Executing ...\x1b[0m\r\n')
        code = -1
        try:
            with self.ssh:
                for code, out in self.ssh.exec_command_with_stream(
                        self.command, self.env):
                    self.send(out)
        except socket.timeout:
            code = 130
            self.send('\r\n\x1b[31m### Time out\x1b[0m')
        except Exception as e:
            code = 131
            self.send(f'\r\n\x1b[31m### Exception {e}\x1b[0m')
            raise e
        finally:
            self.send_status(code)