python – 检查两个CIDR地址是否相交?
作者:互联网
给定两个CIDR地址为192.168.2.0/14和192.168.2.0/32
如何在“python2.6”中检查两个ip地址是否重叠?
我已经通过netaddr,它允许检查是否
192.168.2.0在CIDR地址192.168.2.0/14中
from netaddr import IPNetwork, IPAddress
bool = IPAddress("192.168.2.0") in IPNetwork("192.168.2.0/14"):
但是如何检查两个CIDR地址?
我找到了一个参考:: How can I check if an ip is in a network in python
解决方法:
使用ipaddr:
>>> import ipaddr
>>> n1 = ipaddr.IPNetwork('192.168.1.0/24')
>>> n2 = ipaddr.IPNetwork('192.168.2.0/24')
>>> n3 = ipaddr.IPNetwork('192.168.2.0/25')
>>> n1.overlaps(n2)
False
>>> n1.overlaps(n3)
False
>>> n2.overlaps(n3)
True
>>> n2.overlaps(n1)
False
标签:cidr,python,ip-address 来源: https://codeday.me/bug/20191007/1868898.html