Reputation: 31
I'm writing a script that gets a /16 and breaks it down into different subnets (/24, 23, 27, etc). I realized that I may run into conflicting subnets, and I'm looking for a way to check that. I haven't found anything within ipcalc or netaddr that addresses this specifically. Thank youf or your help
def subnetting(self, cidrBlock, subnets):
networks = subnets
cidrblock = cidrBlock
assigned_subnets = []
ipnetwork = IPNetwork(cidrblock)
subnet_list = ipnetwork.subnet(int(subnets))
for subnet in subnet_list:
assigned_subnets.append(subnet)
return assigned_subnets
Upvotes: 1
Views: 1659
Reputation: 34704
You can use the ipaddress module that's available since Python 3.3.
import ipaddress
cidr1 = ipaddress.ip_network("10.10.0.0/23")
cidr2 = ipaddress.ip_network("10.10.1.0/24")
print("Conflict?", cidr1.overlaps(cidr2))
Upvotes: 0
Reputation: 51
Try with ipconflict.
ipconflict 10.0.1.0/24 10.0.1.0/22
Output:
conflict found: 10.0.1.0/24 <-> 10.0.1.0/22
Upvotes: 0
Reputation: 6452
To compare two subnets to see if they conflict, you need to apply the smallest mask to both to see if they are equal. If they are equal, then you have a conflict.
I will assume the 10.10.0.0/16
network. If you create 10.10.0.0/23
and 10.10.1.0/24
, you compare them by applying the mask for /23
(255.255.254.0
) to both 10.10.0.0
and 10.10.1.0
.
10.10.0.0 AND 255.255.254.0 = 10.10.0.0
10.10.1.0 AND 255.255.254.0 = 10.10.0.0
They are equal, so they overlap and conflict.
Upvotes: 1