Python program to find if two IP Address belongs to Same or Different Network
Prerequisites: Classless Inter Domain Routing (CIDR)
Given two IP Addresses in CIDR Notation determine whether they belong to Same Network or Different Network. Two IP addresses are said to be in Same Network if the Network ID of both the IP Addresses are same.
Input : IP1 = 192.168.1.0/8, IP2 = 184.108.40.206/8
Output : Same Network
Explanation : The number of bits in Network ID of both the IP Addresses is 8 bits and the first
8 bits (i.e 1st octet) of both the IP Addresses is same (i.e 192). Hence both IP Addresses
belong to Same Network.
Input : IP1 = 220.127.116.11/20, IP2 = 18.104.22.168/20
Output : Different Network
Explanation : The number of bits in Network ID of both the IP Addresses is 20 bits but the first
20 bits of both the IP Addresses are different. Hence both IP Addresses belong to Different
To implement it, we will use ip_network and network_address method of ipaddress module of Python 3.3 .
Same Network Different Network