Check if two CIDR addresses intersect?

后端 未结 4 2047
时光说笑
时光说笑 2020-12-16 14:40

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

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

I have gone through netaddr and

4条回答
  •  星月不相逢
    2020-12-16 14:59

    If don't have netaddr at hand for testing, but I think you could check if the first and last address of the first network are both contained in the second:

    net_1 = IPNetwork("192.168.2.0/14")
    net_2 = IPNetwork("192.168.2.0/32")
    if net_1.first in net_2 and net_1.last in net_2:
        # do something
    

    BTW, IPNetwork line 1102 defines a __contains__ method. But I'm not certain the line 1127 isn't broken ? You should test and report a bug if it is.

提交回复
热议问题