Ejemplo n.º 1
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.º 2
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