def optimize_network(self): print ('\nOptimizing network...') lines="" for ip in self.final_list: ip=ip.strip('\n') startip = ip.split('\t')[0] endip = ip.split('\t')[1] cidrs = netaddr.iprange_to_cidrs(startip, endip) for k, cidr in enumerate(cidrs): self.cidr.append(cidr) for line in self.cidr: lines += str(line)+"," lines=lines.rstrip(",") obj=cidrize.optimize_network_range(lines) f=open(self.filename,'a') for new_cidr in cidrize.output_str(obj).split(", "): ip=netaddr.IPNetwork(new_cidr) start=str(netaddr.IPAddress(ip.first)).strip('\n') end=str(netaddr.IPAddress(ip.last)).strip('\n') f.write('%s\t%s\n'%(start,end)) f.close() print ('Saved %d results as:%s'%(len(cidrize.output_str(obj).split(", ")),self.filename))
def validate_cidr(cidr): """ Made for use with jquery.autocomplete. Two matches are returned separated by newlines in the format "network|status": 1. The precise calculated networks based on the input range, (Strict) 2. The maximum spanning CIDR between the first and last IPs. (Loose) Example: input: 16.17.18.19-29 output: 16.17.18.19/32, 16.17.18.20/30, 16.17.18.24/30, 16.17.18.28/31|ok 16.17.18.16/28|ok """ mycidr = output_str(cidr) if not re.match(r"^\d+", mycidr): return "|%s" % mycidr return mycidr + "|ok"
def validate_cidr(cidr): """ Made for use with jquery.autocomplete. Two matches are returned separated by newlines in the format "network|status": 1. The precise calculated networks based on the input range, (Strict) 2. The maximum spanning CIDR between the first and last IPs. (Loose) Example: input: 16.17.18.19-29 output: 16.17.18.19/32, 16.17.18.20/30, 16.17.18.24/30, 16.17.18.28/31|ok 16.17.18.16/28|ok """ mycidr = output_str(cidr) if not re.match(r'^\d+', mycidr): return "|%s" % mycidr return mycidr + "|ok"
def test_output_str(self): cidr = cidrize.cidrize('10.20.30.40-50', strict=True) sep = ', ' expected = '10.20.30.40/29, 10.20.30.48/31, 10.20.30.50/32' self.assertEqual(expected, cidrize.output_str(cidr, sep))
def test_range6(self): cidr = cidrize.cidrize('2001:1234::0.0.64.0-2001:1234::FFff') sep = ', ' expected = '2001:1234::4000/114, 2001:1234::8000/113' self.assertEqual(expected, cidrize.output_str(cidr, sep))