Ejemplo n.º 1
0
    def on_message(self, message):
        role_name = self.get_argument('role', 'sb')
        asset_id = self.get_argument('id', 9999)
        asset = get_object(Asset, id=asset_id)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')
        res = gen_resource({
            'user': self.user,
            'asset': self.assets,
            'role': self.role
        })
        self.runner = MyRunner(res)
        pattern = asset.ip.encode("utf-8")
        import time
        from collections import deque
        while True:
            self.runner.run('shell', 'cat  /home/admin/log', pattern=pattern)
            result = self.runner.results['ok']
            # print result,99999999

            self.write_message(result)
            if result:
                time.sleep(0.5)
                continue
Ejemplo n.º 2
0
    def open(self):
        logger.debug('Websocket: Open exec request')
        role_name = self.get_argument('role', 'sb')
        self.remote_ip = self.request.headers.get("X-Real-IP")
        if not self.remote_ip:
            self.remote_ip = self.request.remote_ip
        logger.debug('Web执行命令: 请求系统用户 %s' % role_name)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')

        res = gen_resource({
            'user': self.user,
            'asset': self.assets,
            'role': self.role
        })
        self.runner = MyRunner(res)
        message = '有权限的主机: ' + ', '.join(
            [asset.hostname for asset in self.assets])
        self.__class__.clients.append(self)
        self.write_message(message)
Ejemplo n.º 3
0
    def open(self):
        logger.debug('Websocket: Open exec request')
        role_name = self.get_argument('role', 'sb')
        self.remote_ip = self.request.remote_ip
        logger.debug('Web执行命令: 请求系统用户 %s' % role_name)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')

        res = gen_resource({'user': self.user, 'asset': self.assets, 'role': self.role})
        self.runner = MyRunner(res)
        message = '有权限的主机: ' + ', '.join([asset.hostname for asset in self.assets])
        self.__class__.clients.append(self)
        self.write_message(message)
Ejemplo n.º 4
0
    def open(self):
        logger.debug("Websocket: Open exec request")
        role_name = self.get_argument("role", "sb")
        self.remote_ip = self.request.headers.get("X-Real-IP")
        if not self.remote_ip:
            self.remote_ip = self.request.remote_ip
        logger.debug("Web执行命令: 请求系统用户 %s" % role_name)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get("role").keys()
        if self.role not in roles:
            self.write_message("No perm that role %s" % role_name)
            self.close()
        self.assets = self.perm.get("role").get(self.role).get("asset")

        res = gen_resource({"user": self.user, "asset": self.assets, "role": self.role})
        self.runner = MyRunner(res)
        message = "有权限的主机: " + ", ".join([asset.hostname for asset in self.assets])
        self.__class__.clients.append(self)
        self.write_message(message)
Ejemplo n.º 5
0
class ExecHandler(tornado.websocket.WebSocketHandler):
    clients = []
    tasks = []

    def __init__(self, *args, **kwargs):
        self.id = 0
        self.user = None
        self.role = None
        self.runner = None
        self.assets = []
        self.perm = {}
        self.remote_ip = ''
        super(ExecHandler, self).__init__(*args, **kwargs)

    def check_origin(self, origin):
        return True

    @django_request_support
    @require_auth('user')
    def open(self):
        logger.debug('Websocket: Open exec request')
        role_name = self.get_argument('role', 'sb')
        self.remote_ip = self.request.headers.get("X-Real-IP")
        if not self.remote_ip:
            self.remote_ip = self.request.remote_ip
        logger.debug('Web执行命令: 请求系统用户 %s' % role_name)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')

        res = gen_resource({
            'user': self.user,
            'asset': self.assets,
            'role': self.role
        })
        self.runner = MyRunner(res)
        message = '有权限的主机: ' + ', '.join(
            [asset.hostname for asset in self.assets])
        self.__class__.clients.append(self)
        self.write_message(message)

    def on_message(self, message):
        data = json.loads(message)
        pattern = data.get('pattern', '')
        self.command = data.get('command', '')
        self.asset_name_str = ''
        if pattern and self.command:
            for inv in self.runner.inventory.get_hosts(pattern=pattern):
                self.asset_name_str += '%s ' % inv.name
            self.write_message('匹配主机: ' + self.asset_name_str)
            self.write_message(
                '<span style="color: yellow">Ansible> %s</span>\n\n' %
                self.command)
            self.__class__.tasks.append(
                MyThread(target=self.run_cmd, args=(self.command, pattern)))

        for t in self.__class__.tasks:
            if t.is_alive():
                continue
            try:
                t.setDaemon(True)
                t.start()
            except RuntimeError:
                pass

    def run_cmd(self, command, pattern):
        self.runner.run('shell', command, pattern=pattern)
        ExecLog(host=self.asset_name_str,
                cmd=self.command,
                user=self.user.username,
                remote_ip=self.remote_ip,
                result=self.runner.results).save()
        newline_pattern = re.compile(r'\n')
        print self.runner.results.items()
        for k, v in self.runner.results.items():
            for host, output in v.items():
                output = newline_pattern.sub('<br />', output)
                if k == 'ok':
                    header = "<span style='color: green'>[ %s => %s]</span>\n" % (
                        host, 'Ok')
                else:
                    header = "<span style='color: red'>[ %s => %s]</span>\n" % (
                        host, 'failed')
                self.write_message(header)
                self.write_message(output)

        self.write_message('\n~o~ Task finished ~o~\n')

    def on_close(self):
        logger.debug('关闭web_exec请求')
Ejemplo n.º 6
0
class ExecHandler(tornado.websocket.WebSocketHandler):
    clients = []
    tasks = []

    def __init__(self, *args, **kwargs):
        self.id = 0
        self.user = None
        self.role = None
        self.runner = None
        self.assets = []
        self.perm = {}
        self.remote_ip = ''
        super(ExecHandler, self).__init__(*args, **kwargs)

    def check_origin(self, origin):
        return True

    @django_request_support
    @require_auth('user')
    def open(self):
        logger.debug('Websocket: Open exec request')
        role_name = self.get_argument('role', 'sb')
        self.remote_ip = self.request.headers.get("X-Real-IP")
        if not self.remote_ip:
            self.remote_ip = self.request.remote_ip
        logger.debug('Web执行命令: 请求系统用户 %s' % role_name)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')

        res = gen_resource({'user': self.user, 'asset': self.assets, 'role': self.role})
        self.runner = MyRunner(res)
        message = '有权限的主机: ' + ', '.join([asset.hostname for asset in self.assets])
        self.__class__.clients.append(self)
        self.write_message(message)

    def on_message(self, message):
        data = json.loads(message)
        pattern = data.get('pattern', '')
        self.command = data.get('command', '')
        self.asset_name_str = ''
        if pattern and self.command:
            for inv in self.runner.inventory.get_hosts(pattern=pattern):
                self.asset_name_str += '%s ' % inv.name
            self.write_message('匹配主机: ' + self.asset_name_str)
            self.write_message('<span style="color: yellow">Ansible> %s</span>\n\n' % self.command)
            self.__class__.tasks.append(MyThread(target=self.run_cmd, args=(self.command, pattern)))

        for t in self.__class__.tasks:
            if t.is_alive():
                continue
            try:
                t.setDaemon(True)
                t.start()
            except RuntimeError:
                pass

    def run_cmd(self, command, pattern):
        self.runner.run('shell', command, pattern=pattern)
        ExecLog(host=self.asset_name_str, cmd=self.command, user=self.user.username,
                remote_ip=self.remote_ip, result=self.runner.results).save()
        newline_pattern = re.compile(r'\n')
        for k, v in self.runner.results.items():
            for host, output in v.items():
                output = newline_pattern.sub('<br />', output)
                if k == 'ok':
                    header = "<span style='color: green'>[ %s => %s]</span>\n" % (host, 'Ok')
                else:
                    header = "<span style='color: red'>[ %s => %s]</span>\n" % (host, 'failed')
                self.write_message(header)
                self.write_message(output)

        self.write_message('\n~o~ Task finished ~o~\n')

    def on_close(self):
        logger.debug('关闭web_exec请求')
Ejemplo n.º 7
0
class MyLogHandler(tornado.websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        self.id = 0
        self.user = None
        self.role = None
        self.runner = None
        self.assets = []
        self.perm = {}
        self.remote_ip = ''
        super(MyLogHandler, self).__init__(*args, **kwargs)

    @django_request_support
    @require_auth('user')
    def open(self, *args):
        # logger.debug('Websocket: Open exec request')
        role_name = self.get_argument('role', 'sb')
        # logger.debug('Web执行命令: 请求系统用户 %s' % role_name)
        self.role = get_object(PermRole, name=role_name)
        # print self.role,111111111111111111
        # self.perm = get_group_user_perm(self.user)
        # roles = self.perm.get('role').keys()
        # if self.role not in roles:
        #     self.write_message('No perm that role %s' % role_name)
        #     self.close()
        # self.assets = self.perm.get('role').get(self.role).get('asset')
        #
        # res = gen_resource({'user': self.user, 'asset': self.assets, 'role': self.role})
        # self.runner = MyRunner(res)
        # message = '有权限的主机: ' + ', '.join([asset.hostname for asset in self.assets])
        #
        # self.write_message(message)

    def on_message(self, message):
        role_name = self.get_argument('role', 'sb')
        asset_id = self.get_argument('id', 9999)
        asset = get_object(Asset, id=asset_id)
        self.role = get_object(PermRole, name=role_name)
        self.perm = get_group_user_perm(self.user)
        roles = self.perm.get('role').keys()
        if self.role not in roles:
            self.write_message('No perm that role %s' % role_name)
            self.close()
        self.assets = self.perm.get('role').get(self.role).get('asset')
        res = gen_resource({
            'user': self.user,
            'asset': self.assets,
            'role': self.role
        })
        self.runner = MyRunner(res)
        pattern = asset.ip.encode("utf-8")
        import time
        from collections import deque
        while True:
            self.runner.run('shell', 'cat  /home/admin/log', pattern=pattern)
            result = self.runner.results['ok']
            # print result,99999999

            self.write_message(result)
            if result:
                time.sleep(0.5)
                continue

    def on_close(self):
        print "Connection closed"