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 = 22.214.171.124/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 = 126.96.36.199/20, IP2 = 188.8.131.52/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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course