Reputation: 2865
I currently have a very large list of IP's, which I am attempting to reduce as much as is feasibly possible. Using the netaddr modules' cidr_merge()
method, I am expecting my list to be drastically condensed. However, it doesn't appear to be working as expected.
For example, given the following IP's I would expect them to be merged as shown:
5.8.183.1
5.8.183.2
5.8.183.5
5.8.183.6
5.8.183.10
5.8.183.14
5.8.183.18
5.8.183.22
5.8.183.26
5.8.183.30
5.8.183.34
5.8.183.38
5.8.183.42
5.8.183.46
Merged into: 5.8.183.0/26
My actual results from the cidr_merge()
method are:
5.8.183.1/32
5.8.183.2/32
5.8.183.5/32
5.8.183.6/32
5.8.183.10/32
5.8.183.14/32
5.8.183.18/32
5.8.183.22/32
5.8.183.26/32
5.8.183.30/32
5.8.183.34/32
5.8.183.38/32
5.8.183.42/32
5.8.183.46/32
Here is my code:
from netaddr import *
try:
with open('nodes', 'r') as in_file:
dat_ips = [IPNetwork(line) for line in in_file.read().splitlines()]
dat_merged_ips = cidr_merge(dat_ips)
with open('output.txt', 'w') as out_file:
for x in dat_merged_ips:
out_file.write(str(x) + '\n')
except IOError:
print('File error detected:')
Upvotes: 0
Views: 851
Reputation: 53
Based on the list of IP addresses you provided, netaddr and your script seem to be working correctly. You need adjacent ip addresses to summarize. Take this node file for example:
5.8.183.1
5.8.183.2
5.8.183.3
5.8.183.4
5.8.183.5
5.8.183.6
5.8.183.7
5.8.183.8
The output from your script is:
[IPNetwork('5.8.183.1/32'), IPNetwork('5.8.183.2/31'), IPNetwork('5.8.183.4/30')]
Take this node list:
5.8.183.1
5.8.183.2
5.8.183.3
5.8.183.4
5.8.183.5
5.8.183.6
5.8.183.7
5.8.183.8
5.8.183.9
5.8.183.10
5.8.183.11
5.8.183.12
5.8.183.13
5.8.183.14
5.8.183.15
Output:
[IPNetwork('5.8.183.1/32'), IPNetwork('5.8.183.2/31'), IPNetwork('5.8.183.4/30'), IPNetwork('5.8.183.8/29')]
Upvotes: 1