Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python IP range to IP range match

Is there any method to check if some IP addresses from a network range are present in the subnets of another IP range?

Example: 10.0.1.0/18 in 123.1.0.0/8 

If it exists, I need it to return True, else False.

like image 630
Ssravankumar Aditya Avatar asked Aug 12 '26 05:08

Ssravankumar Aditya


2 Answers

Since Python 3.3, you can use the ipaddress module of the standard library:

from ipaddress import IPv4Address, IPv4Network

IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
# True
IPv4Address('10.0.1.0') in IPv4Network('192.0.2.0/28')
# False

If you mean if the networks overlap, use overlaps:

In [14]: IPv4Network('10.0.1.0/24').overlaps(IPv4Network('192.0.2.0/28'))
Out[14]: False

Note that this module was marked as provisional in Python 3.3, but no longer is in 3.6. So enjoy it!

like image 89
Thierry Lathuille Avatar answered Aug 14 '26 20:08

Thierry Lathuille


from ipaddress import ip_address, ip_network

NOTE: Other answers specifically point at IPv4Address, IPv4Network, but ip_address and ip_network simply call the IPv4 OR IPv6 version when needed.

addr = ip_address('10.0.1.0')

NOTE: the sample network you provided is not a true /8 network, so the ip_network will respond with an error of ValueError: 123.1.0.0/8 has host bits set

To get past this, by forcing the ipaddress library to essentially round down to the nearest network address, you can set strict=False and Python will find the nearest, lower address for a /8 network: 123.0.0.0

netw = ip_network('123.1.0.0/8', strict=False)

print(addr in netw)

Then it is simply a matter of testing using the in keyword.

If you want to test every address in a network range against every address in another network range, then:

net1 = ip_network('10.0.1.0/18', strict=False)
net2 = ip_network('123.1.0.0/8', strict=False)

for addr in net1:
    if addr in net2:
        print(addr, 'True')
    else:
        print(addr, 'False')

In addition, networks can be converted into sets:

n1 = set(net1)
n2 = set(net2)

And Python sets have the ability show any overlap OR inclusion (subset/superset relationships).

n1.isdisjoint(n2)
n1.issubset(n2)
n2.issubset(n1)
n2.issuperset(n1)

etc.

like image 33
E. Ducateme Avatar answered Aug 14 '26 18:08

E. Ducateme