示例#1
0
    def save_ip(self, data):
        '''保存ip资产相关的结果
        '''
        ip_app = Ip()
        port_app = Port()
        port_attr_app = PortAttr()
        result = {'ip': len(data), 'port': 0}

        for ip in data:
            # 保存IP
            if 'ip' not in ip:
                continue
            if self.org_id:
                ip['org_id'] = self.org_id
            ip_id = ip_app.save_and_update(ip)
            if ip_id > 0:
                result['port'] += len(ip['port'])
                # 保存每个端口数据
                if 'port' not in ip:
                    continue
                for port in ip['port']:
                    port['ip_id'] = ip_id
                    port_id = port_app.save_and_update(port)
                    if port_id > 0:
                        # 保存端口的属性
                        for attr_key in self.result_attr_keys:
                            if attr_key in port and port[attr_key]:
                                data_port_attr = {
                                    'r_id': port_id, 'source': self.source, 'tag': attr_key, 'content': port[attr_key]}
                                port_attr_app.save_and_update(data_port_attr)

        return result
示例#2
0
    def save(self, data):
        '''保存IP归属地结果到数据库
        只更新ip表的location字段
        '''
        ip_app = Ip()
        count = 0
        for ip in data:
            if 'location' in ip and ip['location']:
                if self.org_id:
                    ip['org_id'] = self.org_id
                count += 1 if ip_app.save_and_update(ip) > 0 else 0

        return count
示例#3
0
    def save_ip(self, data):
        '''保存ip资产相关的结果
        '''
        ip_app = Ip()
        port_app = Port()
        port_attr_app = PortAttr()
        result = {'ip': len(data), 'port': 0}

        for ip in data:
            # 保存IP
            if 'ip' not in ip:
                continue
            if self.org_id:
                ip['org_id'] = self.org_id
            ip_id = ip_app.save_and_update(ip)
            if ip_id > 0:
                # 保存每个端口数据
                if 'port' not in ip:
                    continue
                result['port'] += len(ip['port'])
                for port in ip['port']:
                    port['ip_id'] = ip_id
                    try:
                        port_id = port_app.save_and_update(port)
                        if port_id > 0:
                            # 保存端口的属性
                            for attr_key in self.result_attr_keys:
                                if attr_key in port and port[attr_key]:
                                    data_port_attr = {
                                        'r_id': port_id, 'source': self.source, 'tag': attr_key,
                                        'content': port[attr_key][:800]}
                                    try:
                                        port_attr_app.save_and_update(data_port_attr)
                                    except Exception as e:
                                        logger.error(traceback.format_exc())
                                        logger.error('save port port attr:{}-{}-{}-{}'.format(ip['port'], port['port'],
                                                                                            data_port_attr['tag'],
                                                                                            data_port_attr['content']))
                    except Exception as ex:
                        logger.error(traceback.format_exc())
                        logger.error('save ip port:{}-{}'.format(ip['port'], port['port']))
        return result