def save_info(self, ips, source, crawl_time): start = time.time() all_count = len(ips) avail_count = 0 _time = datetime.now().strftime("%Y-%m-%d") if len(ips) > 0: try: for ip, description, area, source in ips: flag = db.session.query(IP).filter( IP.ip == ip, IP.source == source).first() if flag is None: new_ip = IP() new_ip.ip = ip new_ip.description = description new_ip.area = area new_ip.source = source db.session.add(new_ip) avail_count += 1 else: flag.updatetime = _time db.session.add(flag) db.session.commit() except Exception as e: self.logger.warning("Error writing to database" + str(e) + source) else: self.logger.warning("NO record found from: %s" % source) stop = time.time() storage_time = str(stop - start) + "秒" self.logger.info("alienvault 共收集{0}条数据, 新数据{1}条".format( all_count, avail_count)) self.logger.info("alienvault 抓取时间{0},数据遍历时间{1}".format( crawl_time, storage_time))
def Add_Data_To_Url(urls): for url in urls: try: res = Get_Url_Info(url).get_info() res_url = res.get('url') res_title = res.get('title') res_power = res.get('power') res_server = res.get('server') Other_Url.objects.create(url=res_url, title=res_title, power=res_power, server=res_server) except Exception as e: print('错误代码 [29] {}'.format(str(e))) Error_Log.objects.create(url=url, error='错误代码 [29] {}'.format(str(e))) try: ip = get_host(url) if ip == '获取失败': return print('URL --> {} IP --> {}'.format(url, ip)) URL.objects.create(url=url, ip=ip) test_ip = list(IP.objects.filter(ip=ip)) if test_ip != []: return IP_Res = Get_Ip_Info(ip) servers = IP_Res.get_server_from_nmap(ip) # 服务与端口 字典类型 open_port = servers.keys() check_alive_url = [] for port in open_port: check_alive_url.append('http://{}:{}'.format(ip, port)) check_alive_url.append('https://{}:{}'.format(ip, port)) alive_url = Get_Alive_Url(check_alive_url) # 该IP上存活WEB,类型为列表,内容为多个字典 host_type = IP_Res.get_host_type(ip) # windows/linux area = IP_Res.get_ip_address(ip) # 返回地址 IP_Obj = IP() IP_Obj.ip = ip IP_Obj.servers = str(servers) IP_Obj.host_type = host_type IP_Obj.alive_urls = str(alive_url) IP_Obj.area = area try: print(ip, servers, host_type, area) except Exception as e: print('错误代码 [34] {}'.format(str(e))) Error_Log.objects.create(url=url, error='错误代码 [34] {}'.format(str(e))) IP_Obj.save() except Exception as e: print('错误代码 [30] {}'.format(str(e))) Error_Log.objects.create(url=url, error='错误代码 [30] {}'.format(str(e)))