def get_ip(self): if config.USE_IPV6: return random.choice(self.ipv6_list) else: while True: index = random.randint(0, len(self.ip_range_list) - 1) ip_range = self.ip_range_list[index] #xlog.debug("random.randint %d - %d", ip_range[0], ip_range[1]) if ip_range[1] == ip_range[0]: return ip_utils.ip_num_to_string(ip_range[1]) try: id_2 = random.randint(0, ip_range[1] - ip_range[0]) except Exception as e: xlog.exception("random.randint:%r %d - %d, %d", e, ip_range[0], ip_range[1], ip_range[1] - ip_range[0]) return ip = ip_range[0] + id_2 add_last_byte = ip % 256 if add_last_byte == 0 or add_last_byte == 255: continue return ip_utils.ip_num_to_string(ip)
def generate_ip_range(): ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) PRINT("Good ip range:\n") print_range_list(ip_range_list) bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) PRINT("Bad ip range:\n") print_range_list(ip_range_list) ip_range_list = filter_ip_range(ip_range_list, bad_range_list) PRINT("Output ip range:\n") print_range_list(ip_range_list) # write out fd = open("ip_range.txt", "w") for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] #print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write( ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end) + "\n") fd.close()
def generate_ip_range(feature='', good_range=None): if not feature: feature = raw_input( u"\nIP段格式: 1、x.x.x.x-x.x.x.x 2、x.x.x.x/xx ".encode("GBK") ) if feature == '': feature = 1 print("\nMerge Good ip range:") if good_range: ip_range_list = parse_range_string(good_range) else: ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) print("\nMerge Bad ip range:") bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) print("\nCut Bad ip range:") ip_range_list = filter_ip_range(ip_range_list, bad_range_list) # write out ip_out_lists = [] for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] ip_out_lists.append(ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end)) if int(feature) == 2: ip_out_lists = ip_range_to_cidr(ip_out_lists) open('googleip.txt', 'w').write('\n'.join(x for x in ip_out_lists) + '\n')
def merge_range(input_ip_range_list): output_ip_range_list = [] range_num = len(input_ip_range_list) last_begin = input_ip_range_list[0][0] last_end = input_ip_range_list[0][1] for i in range(1,range_num): ip_range = input_ip_range_list[i] begin = ip_range[0] end = ip_range[1] #print "now:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if begin > last_end + 1: #print "add:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) output_ip_range_list.append([last_begin, last_end]) last_begin = begin last_end = end else: print "merge:", ip_utils.ip_num_to_string(last_begin), ip_utils.ip_num_to_string(last_end), ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if end > last_end: last_end = end output_ip_range_list.append([last_begin, last_end]) return output_ip_range_list
def merge_range(input_ip_range_list): output_ip_range_list = [] range_num = len(input_ip_range_list) last_begin = input_ip_range_list[0][0] last_end = input_ip_range_list[0][1] for i in range(1, range_num): ip_range = input_ip_range_list[i] begin = ip_range[0] end = ip_range[1] #print "now:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if begin > last_end + 2: #print "add:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) output_ip_range_list.append([last_begin, last_end]) last_begin = begin last_end = end else: print "merge:", ip_utils.ip_num_to_string( last_begin), ip_utils.ip_num_to_string( last_end), ip_utils.ip_num_to_string( begin), ip_utils.ip_num_to_string(end) if end > last_end: last_end = end output_ip_range_list.append([last_begin, last_end]) return output_ip_range_list
def test_load(): ip_str = [] print("\nBegin test load googleip.txt") fd = open("googleip.txt", "r") if not fd: print "open googleip.txt fail." exit() amount = 0 for line in fd.readlines(): if len(line) == 0 or line[0] == '#': continue begin, end = ip_utils.split_ip(line) ip_begin_str = begin.strip().split('.') ip_end_str = end.strip().split('.') if ip_begin_str[3] == '0': ip_begin_str[3] = '1' if ip_end_str[3] == '255': ip_end_str[3] = '254' str_1 = (int(ip_end_str[0]) - int(ip_begin_str[0])) * 16646144 str_2 = (int(ip_end_str[1]) - int(ip_begin_str[1])) * 65024 str_3 = (int(ip_end_str[2]) - int(ip_begin_str[2])) * 254 str_4 = int(ip_end_str[3]) - int(ip_begin_str[3]) + 1 num = str_1 + str_2 + str_3 + str_4 amount += num nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num fd.close() print "amount ip:", amount
def generate_ip_range(feature='', good_range=None): if not feature: feature = raw_input( u"\nIP段格式: 1、x.x.x.x-x.x.x.x 2、x.x.x.x/xx ".encode(coding) ) if feature == '': feature = 1 print("\nMerge Good ip range:") if good_range: ip_range_list = parse_range_string(good_range) else: ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) print("\nMerge Bad ip range:") bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) print("\nCut Bad ip range:") ip_range_list = filter_ip_range(ip_range_list, bad_range_list) # write out ip_out_lists = [] for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] ip_out_lists.append(ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end)) if int(feature) == 2: ip_out_lists = ip_range_to_cidr(ip_out_lists) open('googleip.txt', 'w').write('\n'.join(x for x in ip_out_lists) + '\n')
def generate_ip_range(): # load input good ip range file_name = "ip_range.txt" input_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name) with open(input_file, "r") as inf: input_good_range_lines = inf.read() ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) PRINT("Good ip range:\n") print_range_list(ip_range_list) if False: input_bad_ip_range_lines = load_bad_ip_range() bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) PRINT("Bad ip range:\n") print_range_list(ip_range_list) ip_range_list = filter_ip_range(ip_range_list, bad_range_list) PRINT("Output ip range:\n") print_range_list(ip_range_list) # write out output_file = os.path.join(config.DATA_PATH, file_name) fd = open(output_file, "w") for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] #print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write(ip_utils.ip_num_to_string(begin)+ "-" + ip_utils.ip_num_to_string(end)+"\n") fd.close()
def scan_ip_worker(self): while self.searching_thread_count <= self.scan_ip_thread_num and connect_control.keep_running: if not connect_control.allow_scan(): time.sleep(10) continue try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if ip_str in self.ip_dict: continue connect_control.start_connect_register() result = check_ip.test_gae(ip_str) connect_control.end_connect_register() if not result: continue if self.add_ip(ip_str, result.handshake_time, result.domain, result.server_type): #logging.info("add %s CN:%s type:%s time:%d gws:%d ", ip_str, # result.domain, result.server_type, result.handshake_time, len(self.gws_ip_list)) xlog.info("scan_ip add ip:%s time:%d", ip_str, result.handshake_time) scan_ip_log.info("Add %s time:%d CN:%s type:%s", ip_str, result.handshake_time, result.domain, result.server_type) self.remove_slowest_ip() self.save_ip_list() except Exception as e: xlog.exception("google_ip.runJob fail:%s", e) self.ncount_lock.acquire() self.searching_thread_count -= 1 self.ncount_lock.release() xlog.info("scan_ip_worker exit")
def get_ip(self): while True: ip_range = random.choice(self.ip_range_list) if ip_range[1] == ip_range[0]: return ip_utils.ip_num_to_string(ip_range[1]) try: id_2 = random.randint(0, ip_range[1] - ip_range[0]) except Exception as e: xlog.exception("random.randint:%r %d - %d, %d", e, ip_range[0], ip_range[1], ip_range[1] - ip_range[0]) return ip = ip_range[0] + id_2 return ip_utils.ip_num_to_string(ip)
def test_main(): #ip_range_manager = google_ip_range.ip_range() while True: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) test(ip_str, 1)
def test(): proxy = ip_range() # proxy.show_ip_range() for i in range(1, 300): ip = proxy.get_ip() ip_addr = ip_utils.ip_num_to_string(ip) print "ip:", ip_addr
def scan_ip_worker(self): while self.searching_thread_count <= self.scan_ip_thread_num and connect_control.keep_running: if not connect_control.allow_scan(): time.sleep(10) continue try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if ip_str in self.ip_dict: continue connect_control.start_connect_register() result = check_ip.test_gae_ip(ip_str) connect_control.end_connect_register() if not result: continue if self.add_ip(ip_str, result.handshake_time, result.domain, "gws"): #logging.info("add %s CN:%s type:%s time:%d gws:%d ", ip_str, # result.domain, result.server_type, result.handshake_time, len(self.gws_ip_list)) xlog.info("scan_ip add ip:%s time:%d", ip_str, result.handshake_time) scan_ip_log.info("Add %s time:%d CN:%s ", ip_str, result.handshake_time, result.domain) self.remove_slowest_ip() self.save_ip_list() except Exception as e: xlog.exception("google_ip.runJob fail:%r", e) self.ncount_lock.acquire() self.searching_thread_count -= 1 self.ncount_lock.release() xlog.info("scan_ip_worker exit")
def scan_ip_worker(self): while self.searching_thread_count <= self.scan_ip_thread_num: if not connect_control.allow_scan(): time.sleep(10) continue try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if self.is_bad_ip(ip_str): continue result = check_ip.test_gws(ip_str) if not result: continue if self.add_ip(ip_str, result.handshake_time, result.domain, result.server_type): #logging.info("add %s CN:%s type:%s time:%d gws:%d ", ip_str, # result.domain, result.server_type, result.handshake_time, len(self.gws_ip_list)) logging.info("scan_ip add ip:%s time:%d", ip_str, result.handshake_time) scan_ip_log.info("Add %s time:%d CN:%s type:%s", ip_str, result.handshake_time, result.domain, result.server_type) self.remove_slowest_ip() self.save_ip_list() except check_ip.HoneypotError as e: self.report_bad_ip(ip_str) connect_control.fall_into_honeypot() continue except Exception as e: logging.exception("google_ip.runJob fail:%s", e) self.ncount_lock.acquire() self.searching_thread_count -= 1 self.ncount_lock.release() logging.info("scan_ip_worker exit")
def runJob(self): while True: #not self.is_ip_enough() and self.searching_thread_count < 2: if not connect_control.allow_connect(): break try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if self.is_bad_ip(ip_str): continue result = check_ip.test_gws(ip_str) if not result: continue if self.add_ip(ip_str, result.handshake_time, result.domain, result.server_type): #logging.info("add %s CN:%s type:%s time:%d gws:%d ", ip_str, # result.domain, result.server_type, result.handshake_time, len(self.gws_ip_list)) logging.info("check_ip add ip:%s time:%d", ip_str, result.handshake_time) self.remove_slowest_ip() self.save_ip_list() except check_ip.HoneypotError as e: self.report_bad_ip(ip_str) connect_control.fall_into_honeypot() break except Exception as e: logging.exception("google_ip.runJob fail:%s", e) self.ncount_lock.acquire() self.searching_thread_count -= 1 self.ncount_lock.release()
def random_get_ip(self): while self.bin_fd is None: time.sleep(1) position = random.randint(0, self.bin_size / 4) * 4 self.bin_fd.seek(position) ip_bin = self.bin_fd.read(4) ip_num = struct.unpack("<I", ip_bin)[0] ip = ip_utils.ip_num_to_string(ip_num) return ip
def runJob(self): while self.check_num < 1000000: try: time.sleep(1) ip_int = ip_range.get_ip() #ip_int = ip_range.random_get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) self.check_ip(ip_str) except Exception as e: logging.warn("google_ip.runJob fail:%s", e)
def runJob(self): while self.check_num < 1000000: try: time.sleep(1) ip_int = ip_range.get_ip() #ip_int = ip_range.random_get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) self.check_ip(ip_str) except Exception as e: xlog.warn("google_ip.runJob fail:%s", e)
def get_ipv4(self): while True: index = random.randint(0, len(self.ip_range_list) - 1) ip_range = self.ip_range_list[index] #xlog.debug("random.randint %d - %d", ip_range[0], ip_range[1]) if ip_range[1] == ip_range[0]: return ip_utils.ip_num_to_string(ip_range[1]) try: id_2 = random.randint(0, ip_range[1] - ip_range[0]) except Exception as e: xlog.exception("random.randint:%r %d - %d, %d", e, ip_range[0], ip_range[1], ip_range[1] - ip_range[0]) return ip = ip_range[0] + id_2 add_last_byte = ip % 256 if add_last_byte == 0 or add_last_byte == 255: continue return ip_utils.ip_num_to_string(ip)
def generate_ip_range(): print("\nMerge Good ip range:") ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) print("\nMerge Bad ip range:") bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) print("\nCut Bad ip range:") ip_range_list = filter_ip_range(ip_range_list, bad_range_list) # write out ip_out_lists = [] for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] ip_out_lists.append(ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end)) ip_out_lists = ip_range_to_cidr(ip_out_lists) open('googleip.txt', 'w').write('\n'.join(x for x in ip_out_lists))
def get_ip(self): #return self.get_real_random_ip() while True: index = random.randint(0, len(self.ip_range_list) - 1) ip_range = self.ip_range_list[index] #logging.debug("random.randint %d - %d", ip_range[0], ip_range[1]) if ip_range[1] == ip_range[0]: return ip_utils.ip_num_to_string(ip_range[1]) try: id_2 = random.randint(0, ip_range[1] - ip_range[0]) except Exception as e: xlog.exception("random.randint:%r %d - %d, %d", e, ip_range[0], ip_range[1], ip_range[1] - ip_range[0]) return ip = ip_range[0] + id_2 add_last_byte = ip % 256 if add_last_byte == 0 or add_last_byte == 255: continue return ip_utils.ip_num_to_string(ip)
def generate_ip_range(): print ("\nMerge Good ip range:") ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) print ("\nMerge Bad ip range:") bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) print ("\nCut Bad ip range:") ip_range_list = filter_ip_range(ip_range_list, bad_range_list) # write out fd = open("googleip.txt", "w") for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] # print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write(ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end) + "\n") fd.close()
def generate_ip_range(): print("\nMerge Good ip range:") ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) print("\nMerge Bad ip range:") bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) print("\nCut Bad ip range:") ip_range_list = filter_ip_range(ip_range_list, bad_range_list) # write out ip_out_lists = [] for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] ip_out_lists.append( ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end)) ip_out_lists = ip_range_to_cidr(ip_out_lists) open('googleip.txt', 'w').write('\n'.join(x for x in ip_out_lists))
def test_load(): print("\nBegin test load googleip.txt") fd = open("googleip.txt", "r") if not fd: print "open googleip.txt fail." exit() amount = 0 for line in fd.readlines(): if len(line) == 0 or line[0] == '#': continue begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) num = nend - nbegin amount += num print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num fd.close() print "amount ip:", amount
def test_load(): fd = open("ip_range.txt", "r") if not fd: print "open ip_range.txt fail." exit() amount = 0 for line in fd.readlines(): if len(line) == 0 or line[0] == '#': continue begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) num = nend - nbegin amount += num print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num fd.close() print "amount:", amount
def test_load(): fd = open("ip_range.txt", "r") if not fd: print "open ip_range.txt fail." exit() amount = 0 for line in fd.readlines(): if len(line) == 0 or line[0] == "#": continue begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) num = nend - nbegin amount += num print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num fd.close() print "amount:", amount
def runJob(self): while not self.is_ip_enough(): try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if self.check_ip(ip_str): self.save_ip_list() except Exception as e: logging.warn("google_ip.runJob fail:%s", e) self.ncount_lock.acquire() self.ncount -= 1 self.ncount_lock.release()
def test_load(): print("Begin test load ip_range.txt\n") file_name = os.path.join(config.DATA_PATH, "ip_range.txt") fd = open(file_name, "r") if not fd: print "open ip_range.txt fail." exit() amount = 0 for line in fd.readlines(): if len(line) == 0 or line[0] == '#': continue begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) num = nend - nbegin amount += num print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num fd.close() print "amount ip:", amount
def generate_ip_range(): ip_range_list = parse_range_string(input_good_range_lines) ip_range_list = merge_range(ip_range_list) PRINT("Good ip range:\n") print_range_list(ip_range_list) bad_range_list = parse_range_string(input_bad_ip_range_lines) bad_range_list = merge_range(bad_range_list) PRINT("Bad ip range:\n") print_range_list(ip_range_list) ip_range_list = filter_ip_range(ip_range_list, bad_range_list) PRINT("Output ip range:\n") print_range_list(ip_range_list) # write out fd = open("ip_range.txt", "w") for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] #print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write(ip_utils.ip_num_to_string(begin)+ "-" + ip_utils.ip_num_to_string(end)+"\n") fd.close()
def runJob(self): while True: #not self.is_ip_enough() and self.searching_thread_count < 2: try: time.sleep(1) ip_int = ip_range.get_ip() ip_str = ip_utils.ip_num_to_string(ip_int) if self.check_ip(ip_str): self.remove_slowest_ip() self.save_ip_list() except Exception as e: logging.exception("google_ip.runJob fail:%s", e) self.ncount_lock.acquire() self.searching_thread_count -= 1 self.ncount_lock.release()
def random_get_ip(self): while self.bin_fd is None: time.sleep(1) for _ in range(5): position = random.randint(0, self.bin_size/4) * 4 self.bin_fd.seek(position) ip_bin = self.bin_fd.read(4) if ip_bin is None: xlog.warn("ip_pool.random_get_ip position:%d get None", position) elif len(ip_bin) != 4: xlog.warn("ip_pool.random_get_ip position:%d len:%d", position, len(ip_bin)) else: ip_num = struct.unpack("<I", ip_bin)[0] ip = ip_utils.ip_num_to_string(ip_num) return ip time.sleep(3) raise Exception("get ip fail.")
def filter_ip_range(good_range, bad_range): out_good_range = [] bad_i = 0 bad_begin, bad_end = bad_range[bad_i] for good_begin, good_end in good_range: while True: if good_begin > good_end: print("bad good ip range when filter:%s-%s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(good_end))) break if good_end < bad_begin: # case: # [ good ] # [ bad ] out_good_range.append([good_begin, good_end]) break elif bad_end < good_begin: # case: # [ good ] # [ bad ] bad_i += 1 if bad_i < len(bad_range): bad_begin, bad_end = bad_range[bad_i] continue else: out_good_range.append([good_begin, good_end]) break elif good_begin <= bad_begin and good_end <= bad_end: # case: # [ good ] # [ bad ] print("cut bad ip case 1:%s - %s" % (ip_utils.ip_num_to_string(bad_begin), ip_utils.ip_num_to_string(good_end))) if bad_begin - 1 > good_begin: out_good_range.append([good_begin, bad_begin - 1]) break elif good_begin >= bad_begin and good_end == bad_end: # case: # [ good ] # [ bad ] print("cut bad ip case 2:%s - %s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(bad_end))) bad_i += 1 if bad_i < len(bad_range): bad_begin, bad_end = bad_range[bad_i] break elif good_begin >= bad_begin and good_end > bad_end: # case: # [ good ] # [ bad ] print("cut bad ip case 3:%s - %s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(bad_end))) good_begin = bad_end + 1 bad_i += 1 if bad_i < len(bad_range): bad_begin, bad_end = bad_range[bad_i] continue else: out_good_range.append([bad_end + 1, good_end]) break elif good_begin <= bad_begin and good_end >= bad_end: # case: # [ good ] # [ bad ] out_good_range.append([good_begin, bad_begin - 1]) print("cut bad ip case 4:%s - %s" % (ip_utils.ip_num_to_string(bad_begin), ip_utils.ip_num_to_string(bad_end))) good_begin = bad_end + 1 bad_i += 1 if bad_i < len(bad_range): bad_begin, bad_end = bad_range[bad_i] continue else: out_good_range.append([bad_end + 1, good_end]) break elif good_begin >= bad_begin and good_end <= bad_end: # case: # [good] # [ bad ] print("cut bad ip case 5:%s - %s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(good_end))) break else: print("any case?") return out_good_range
def print_range_list(ip_range_list): for ip_range in ip_range_list: begin = ip_range[0] end = ip_range[1] print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end)
def merge_ip_range(): ip_range_list = [] ip_lines_list = re.split("\r|\n", ip_str_list) for iplines in ip_lines_list: if len(iplines) == 0 or iplines[0] == '#': #print "non:", iplines continue ips = re.split(",|\|", iplines) for line in ips: if len(line) == 0 or line[0] == '#': #print "non line:", line continue begin, end = ip_utils.split_ip(line) if ip_utils.check_ip_valid(begin) == 0 or ip_utils.check_ip_valid( end) == 0: PRINT("ip format is error,line:%s, begin: %s,end: %s" % (line, begin, end)) continue nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) ip_range_list.append([nbegin, nend]) #print begin, end ip_range_list.sort() # merge range ip_range_list_2 = [] range_num = len(ip_range_list) last_begin = ip_range_list[0][0] last_end = ip_range_list[0][1] for i in range(1, range_num - 1): ip_range = ip_range_list[i] begin = ip_range[0] end = ip_range[1] #print "now:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if begin > last_end + 2: #print "add:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) ip_range_list_2.append([last_begin, last_end]) last_begin = begin last_end = end else: print "merge:", ip_utils.ip_num_to_string( last_begin), ip_utils.ip_num_to_string( last_end), ip_utils.ip_num_to_string( begin), ip_utils.ip_num_to_string(end) if end > last_end: last_end = end ip_range_list_2.append([last_begin, last_end]) for ip_range in ip_range_list_2: begin = ip_range[0] end = ip_range[1] print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) # write out fd = open("ip_range.txt", "w") for ip_range in ip_range_list_2: begin = ip_range[0] end = ip_range[1] #print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write( ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end) + "\n") fd.close()
def report_bad_ip(ip_str): ip_bin = ip_utils.ip_string_to_num(ip_str) ip_bin = ip_bin & 0xffffff00 ip_mask = ip_utils.ip_num_to_string(ip_bin) return ip_mask
def filter_ip_range(good_range, bad_range): out_good_range = [] bad_i = 0 bad_range_num = len(bad_range) bad_begin, bad_end = bad_range[bad_i] for good_begin, good_end in good_range: while True: if good_begin > good_end: PRINT("bad good ip range when filter:%s-%s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(good_end))) if good_end < bad_begin: # case: # [ good ] # [ bad ] out_good_range.append([good_begin, good_end]) break elif bad_end < good_begin: # case: # [ good ] # [ bad ] bad_i += 1 bad_begin, bad_end = bad_range[bad_i] continue elif good_begin <= bad_begin and good_end <= bad_end: # case: # [ good ] # [ bad ] PRINT("cut bad ip case 1:%s - %s" % (ip_utils.ip_num_to_string(bad_begin), ip_utils.ip_num_to_string(good_end))) if bad_begin - 1 > good_begin: out_good_range.append([good_begin, bad_begin - 1]) break elif good_begin >= bad_begin and good_end >= bad_end: # case: # [ good ] # [ bad ] PRINT("cut bad ip case 2:%s - %s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(bad_end))) good_begin = bad_end + 1 bad_i += 1 bad_begin, bad_end = bad_range[bad_i] continue elif good_begin <= bad_begin and good_end >= bad_end: # case: # [ good ] # [ bad ] out_good_range.append([good_begin, bad_begin - 1]) PRINT("cut bad ip case 3:%s - %s" % (ip_utils.ip_num_to_string(bad_begin), ip_utils.ip_num_to_string(bad_end))) good_begin = bad_end + 1 bad_i += 1 bad_begin, bad_end = bad_range[bad_i] continue elif good_begin >= bad_begin and good_end <= bad_end: # case: # [good] # [ bad ] PRINT("cut bad ip case 4:%s - %s" % (ip_utils.ip_num_to_string(good_begin), ip_utils.ip_num_to_string(good_end))) break else: PRINT("any case?") return out_good_range
def merge_ip_range(): ip_range_list = [] ip_lines_list = re.split("\r|\n", ip_str_list) for iplines in ip_lines_list: if len(iplines) == 0 or iplines[0] == "#": # print "non:", iplines continue ips = re.split(",|\|", iplines) for line in ips: if len(line) == 0 or line[0] == "#": # print "non line:", line continue begin, end = ip_utils.split_ip(line) if ip_utils.check_ip_valid(begin) == 0 or ip_utils.check_ip_valid(end) == 0: PRINT("ip format is error,line:%s, begin: %s,end: %s" % (line, begin, end)) continue nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) ip_range_list.append([nbegin, nend]) # print begin, end ip_range_list.sort() # merge range ip_range_list_2 = [] range_num = len(ip_range_list) last_begin = ip_range_list[0][0] last_end = ip_range_list[0][1] for i in range(1, range_num - 1): ip_range = ip_range_list[i] begin = ip_range[0] end = ip_range[1] # print "now:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if begin > last_end + 2: # print "add:",ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) ip_range_list_2.append([last_begin, last_end]) last_begin = begin last_end = end else: print "merge:", ip_utils.ip_num_to_string(last_begin), ip_utils.ip_num_to_string( last_end ), ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) if end > last_end: last_end = end ip_range_list_2.append([last_begin, last_end]) for ip_range in ip_range_list_2: begin = ip_range[0] end = ip_range[1] print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) # write out fd = open("ip_range.txt", "w") for ip_range in ip_range_list_2: begin = ip_range[0] end = ip_range[1] # print ip_utils.ip_num_to_string(begin), ip_utils.ip_num_to_string(end) fd.write(ip_utils.ip_num_to_string(begin) + "-" + ip_utils.ip_num_to_string(end) + "\n") fd.close()