def load_ip_range(self): fd = open("ip_range.txt", "r") if not fd: print "open ip_range.txt fail." exit() self.ip_range_map = {} self.ip_range_list = [] self.ip_range_index = [] self.candidate_amount_ip = 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) self.ip_range_map[self.candidate_amount_ip] = [nbegin, nend] self.ip_range_list.append([nbegin, nend]) self.ip_range_index.append(self.candidate_amount_ip) num = nend - nbegin self.candidate_amount_ip += num # print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num self.ip_range_index.sort() fd.close()
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 test_load(): print("\nBegin test load googleip.txt") fd = open("googleip.txt", "r") i = 1 amount = 0 ip_rip = 10000000 # 以1KW个IP分割IP段 ip_lists = [] ip_range_list = re.split("\r|\n", fd.read()) ip_amount = test_ip_amount(ip_range_list) for line in ip_range_list: if len(line) == 0 or line[0] == '#': continue begin, end = ip_utils.split_ip(line) num = test_ip_num(begin, end) amount += num ip_lists.append(line) #print begin, end, num filename = 'googleip-%03d.txt' % i if amount > i*ip_rip: print "ip amount over %s" % format(i*ip_rip, ',') ip_lists = ip_range_to_cidr(ip_lists) open(filename,'w').write('\n'.join(x for x in ip_lists)) i += 1 ip_lists = [] elif amount == ip_amount: print "ip amount below %s" % format(i*ip_rip, ','), '\n' ip_lists = ip_range_to_cidr(ip_lists) if amount > ip_rip : open(filename,'w').write('\n'.join(x for x in ip_lists)) continue fd.close()
def load_ec2_range(self): fn = os.path.join(current_path, "ec2_iprange.txt") with open(fn, "r") as fd: ipr = json.load(fd) for ipd in ipr["prefixes"]: try: if ipd["region"] != "us-east-1": continue if "ip_prefix" not in ipd: continue if ipd["service"] != "EC2": continue ipp = ipd["ip_prefix"] # xlog.debug("range:%s", ipp) begin, end = ip_utils.split_ip(ipp) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) self.ip_range_list.append([nbegin, nend]) except: continue
def load_ip_range(self): self.ip_range_map = {} self.ip_range_list = [] self.ip_range_index = [] self.candidate_amount_ip = 0 content = self.load_range_content() lines = content.splitlines() for line in lines: if len(line) == 0 or line[0] == '#': continue try: begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) if not nbegin or not nend or nend < nbegin: xlog.warn("load ip range:%s fail", line) continue except Exception as e: xlog.exception("load ip range:%s fail:%r", line, e) continue self.ip_range_map[self.candidate_amount_ip] = [nbegin, nend] self.ip_range_list.append( [nbegin, nend] ) self.ip_range_index.append(self.candidate_amount_ip) num = nend - nbegin self.candidate_amount_ip += num # print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num self.ip_range_index.sort()
def load_ip_range(self): logging.info("load ip range file:%s", self.range_file) fd = open(self.range_file, "r") if not fd: print "open ip_range.txt fail." exit() self.ip_range_map = {} self.ip_range_list = [] self.ip_range_index = [] self.candidate_amount_ip = 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) if not nbegin or not nend or nend < nbegin: logging.error("load ip range:%s", line) self.ip_range_map[self.candidate_amount_ip] = [nbegin, nend] self.ip_range_list.append( [nbegin, nend] ) self.ip_range_index.append(self.candidate_amount_ip) num = nend - nbegin self.candidate_amount_ip += num # print ip_utils.ip_num_to_string(nbegin), ip_utils.ip_num_to_string(nend), num self.ip_range_index.sort() fd.close()
def parse_range_string(input_lines): ip_range_list = [] ip_lines_list = re.split("\r|\n", input_lines) for raw_line in ip_lines_list: raw_s = raw_line.split("#") context_line = raw_s[0] context_line = context_line.replace(' ', '') ips = re.split(",|\|", context_line) for line in ips: if len(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() return ip_range_list
def test_load(): print ("\nBegin test load googleip.txt") fd = open("googleip.txt", "r") i = 1 amount = 0 ip_rip = 10000000 # 以1KW个IP分割IP段 ip_lists = [] ip_range_list = re.split("\r|\n", fd.read()) ip_amount = test_ip_amount(ip_range_list) for line in ip_range_list: if len(line) == 0 or line[0] == "#": continue begin, end = ip_utils.split_ip(line) num = test_ip_num(begin, end) amount += num ip_lists.append([begin, end]) # print begin, end, num if amount > i * ip_rip: filename = "googleip-%03d.txt" % i print "ip amount over %s" % (i * ip_rip) open(filename, "w").write("\n".join(x + "-" + y for x, y in ip_lists)) i += 1 ip_lists = [] elif amount == ip_amount: filename = "googleip-%03d.txt" % i print "ip amount below %s" % (i * ip_rip), "\n" if amount > ip_rip: open(filename, "w").write("\n".join(x + "-" + y for x, y in ip_lists)) continue fd.close()
def parse_range_string(input_lines): ip_range_list = [] ip_lines_list = re.split("\r|\n", input_lines) for raw_line in ip_lines_list: raw_s = raw_line.split("#") context_line = raw_s[0] context_line = context_line.replace(' ', '') ips = re.split(",|\|", context_line) for line in ips: if len(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() return ip_range_list
def test_ip_amount(ip_lists): amount = 0 for ip in ip_lists: begin, end = ip_utils.split_ip(ip) num = test_ip_num(begin, end) amount += num #print begin, end, num print "amount ip: %s \n" % format(amount, ',') return amount
def ip_range_to_cidr(ip_str_lists): ip_cidr_network = [] ip_cidr_lists = [] for ip_str in ip_str_lists: begin, end = ip_utils.split_ip(ip_str) cidrs = netaddr.iprange_to_cidrs(begin, end) for ip in cidrs: ip_cidr_network.append(ip) for ip_cidr in ip_cidr_network: ip_cidr_lists.append(str(ip_cidr)) return ip_cidr_lists
def test_ip_amount(ip_lists): amount = 0 for ip in ip_lists: if len(ip) == 0 or ip[0] == '#': continue begin, end = ip_utils.split_ip(ip) num = test_ip_num(begin, end) amount += num print begin, end, num print "amount ip: %s \n" % format(amount, ',') return amount
def test_ip_amount(ip_lists): amount = 0 for ip in ip_lists: if len(ip) == 0 or ip[0] == "#": continue begin, end = ip_utils.split_ip(ip) num = test_ip_num(begin, end) amount += num print begin, end, num print "amount ip:", amount, "\n" return amount
def test_load(): print("\nBegin test load %s" % googleip_fimename) fd = open(googleip) i = 1 amount = 0 ip_rip = 10000000 # 以1KW个IP分割IP段 ip_lists = [] ip_range_list = [] ip_range = re.split("\r|\n", fd.read()) for ip_line in ip_range: if len(ip_line) == 0 or ip_line[0] == '#': continue ip_line = ip_line.replace(' ', '') ips = ip_line.split("|") for ip in ips: if len(ip) == 0: continue ip_range_list.append(ip) fd.close() ip_amount = test_ip_amount(ip_range_list) if ip_amount > ip_rip: rip = raw_input(u"IP数量超过%s,是否分割IP段: 1.是 2.否 ".encode(coding) % format(i * ip_rip, ',')) else: return rip = rip.strip() if rip == '2' or rip != '1': print return for line in ip_range_list: begin, end = ip_utils.split_ip(line) num = test_ip_num(begin, end) amount += num ip_lists.append(line) filename = '%s-%03d.txt' % (googleip_fimename.split(".")[0], i) if amount > i * ip_rip: print "ip amount over %s" % format(i * ip_rip, ',') ip_lists = ip_range_to_cidr(ip_lists) open(filename, 'w').write('\n'.join(x for x in ip_lists) + '\n') i += 1 ip_lists = [] elif amount == ip_amount: print "ip amount below %s" % format(i * ip_rip, ','), '\n' ip_lists = ip_range_to_cidr(ip_lists) if amount > ip_rip: open(filename, 'w').write('\n'.join(x for x in ip_lists) + '\n')
def test_load(): print("\nBegin test load googleip.txt") fd = open('googleip.txt') i = 1 amount = 0 ip_rip = 10000000 # 以1KW个IP分割IP段 ip_lists = [] ip_range_list = [] ip_range = re.split("\r|\n", fd.read()) for ip_line in ip_range: if len(ip_line) == 0 or ip_line[0] == '#': continue ip_line = ip_line.replace(' ', '') ips = ip_line.split("|") for ip in ips: if len(ip) == 0: continue ip_range_list.append(ip) fd.close() ip_amount = test_ip_amount(ip_range_list) if ip_amount > ip_rip: rip = raw_input( u"IP数量超过%s,是否分割IP段: 1.是 2.否 ".encode("GBK") % format(i*ip_rip, ',') ) else: return rip = rip.strip() if rip == '2' or rip != '1': print return for line in ip_range_list: begin, end = ip_utils.split_ip(line) num = test_ip_num(begin, end) amount += num ip_lists.append(line) filename = 'googleip-%03d.txt' % i if amount > i*ip_rip: print "ip amount over %s" % format(i*ip_rip, ',') ip_lists = ip_range_to_cidr(ip_lists) open(filename,'w').write('\n'.join(x for x in ip_lists) + '\n') i += 1 ip_lists = [] elif amount == ip_amount: print "ip amount below %s" % format(i*ip_rip, ','), '\n' ip_lists = ip_range_to_cidr(ip_lists) if amount > ip_rip : open(filename,'w').write('\n'.join(x for x in ip_lists) + '\n')
def test_ip_amount(file_name,ip_lists): amount = 0 for ip in ip_lists: begin, end = ip_utils.split_ip(ip) ip_begin_str = begin.split('.') ip_end_str = end.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 #print begin, end, num amount = format(amount, ',') print "%s amount ip: %s \n" % (file_name, amount) return amount
def test_load(): print("\nBegin test load googleip.txt") fd = open("googleip.txt", "r") i = 1 amount = 0 ip_rip = 10000000 # 以1KW个IP分割IP段 ip_lists = [] ip_range_list = [] ip_range = re.split("\r|\n", fd.read()) for ip_line in ip_range: if len(ip_line) == 0 or ip_line[0] == '#': continue ip_line = ip_line.replace(' ', '') ips = ip_line.split("|") for ip in ips: if len(ip) == 0: continue ip_range_list.append(ip) ip_amount = test_ip_amount(ip_range_list) for line in ip_range_list: begin, end = ip_utils.split_ip(line) num = test_ip_num(begin, end) amount += num ip_lists.append(line) filename = 'googleip-%03d.txt' % i if amount > i * ip_rip: print "ip amount over %s" % format(i * ip_rip, ',') ip_lists = ip_range_to_cidr(ip_lists) open(filename, 'w').write('\n'.join(x for x in ip_lists)) i += 1 ip_lists = [] elif amount == ip_amount: print "ip amount below %s" % format(i * ip_rip, ','), '\n' ip_lists = ip_range_to_cidr(ip_lists) if amount > ip_rip: open(filename, 'w').write('\n'.join(x for x in ip_lists)) continue fd.close()
def load_ip_range(self): self.ip_range_map = {} self.ip_range_list = [] self.candidate_amount_ip = 0 lines = [] content = self.load_range_content() raw_lines = content.splitlines() for ip_line in raw_lines: ip_line = ip_line.replace(' ', '') if len(ip_line) == 0 or ip_line[0] == '#': continue ips = ip_line.split("|") for ip in ips: if len(ip) == 0: continue lines.append(ip) if config.USE_IPV6: self.ipv6_list = lines return for line in lines: if len(line) == 0 or line[0] == '#': continue try: begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) if not nbegin or not nend or nend < nbegin: xlog.warn("load ip range:%s fail", line) continue except Exception as e: xlog.exception("load ip range:%s fail:%r", line, e) continue self.ip_range_map[self.candidate_amount_ip] = [nbegin, nend] self.ip_range_list.append( [nbegin, nend] ) num = nend - nbegin self.candidate_amount_ip += num
def load_ip_range(self): self.ip_range_map = {} self.ip_range_list = [] self.candidate_amount_ip = 0 lines = [] content = self.load_range_content() raw_lines = content.splitlines() for ip_line in raw_lines: ip_line = ip_line.replace(' ', '') if len(ip_line) == 0 or ip_line[0] == '#': continue ips = ip_line.split("|") for ip in ips: if len(ip) == 0: continue lines.append(ip) if config.USE_IPV6: self.ipv6_list = lines return for line in lines: if len(line) == 0 or line[0] == '#': continue try: begin, end = ip_utils.split_ip(line) nbegin = ip_utils.ip_string_to_num(begin) nend = ip_utils.ip_string_to_num(end) if not nbegin or not nend or nend < nbegin: xlog.warn("load ip range:%s fail", line) continue except Exception as e: xlog.exception("load ip range:%s fail:%r", line, e) continue self.ip_range_map[self.candidate_amount_ip] = [nbegin, nend] self.ip_range_list.append([nbegin, nend]) num = nend - nbegin self.candidate_amount_ip += num
def googleip_to_cidr(): ip_lists = [] ip_range = open("googleip.txt") for line in ip_range.readlines(): line = line.strip("\n") if len(line) == 0 or line[0] == "#": continue begin, end = ip_utils.split_ip(line) cidrs = netaddr.iprange_to_cidrs(begin, end) for k, v in enumerate(cidrs): ip = v ip_lists.append(ip) fd = open("googleip.ip.txt", "w") for ip_cidr in ip_lists: # print ip_cidr fd.write(str(ip_cidr) + "\n") fd.close() print ("\n convert finished!\n")
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(): 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(): 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 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 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()