本文介绍了检查两个CIDR地址是否相交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出两个CIDR地址,例如192.168.2.0/14和192.168.2.0/32

Given two CIDR addresses say 192.168.2.0/14 and 192.168.2.0/32

如何检查 python2.6中是否有两个IP地址重叠? ?

How do I check if two ip addresses overlap in "python2.6"??

我通过netaddr,它可以检查
192.168.2.0是否在CIDR地址192.168.2.0/14中,

I have gone through netaddr and it allows to check if192.168.2.0 is in CIDR address 192.168.2.0/14 by

from netaddr import IPNetwork, IPAddress
bool = IPAddress("192.168.2.0") in IPNetwork("192.168.2.0/14"):

但是如何检查两个CIDR地址?

But how to check for two CIDR address??

我找到了参考:: ::

I found a reference :: How can I check if an ip is in a network in python

推荐答案

使用:

>>> 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地址是否相交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 01:11