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.

Examples:

Input : IP1 = 192.168.1.0/8, IP2 = 192.32.45.7/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 = 17.53.128.0/20, IP2 = 17.53.127.0/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
Network.

To implement it, we will use ip_network and network_address method of ipaddress module of Python 3.3 .

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing ip_network from ipaddress module
from ipaddress import ip_network
    
def sameNetwork(IP1: str, IP2: str) -> str:
    
    a = ip_network(IP1, strict = False).network_address
    b = ip_network(IP2, strict = False).network_address
  
    if(a == b) :
        return "Same Network" 
  
    else :
        return "Different Network"
  
# main function    
if __name__ == '__main__'
   
    # Same Network  
    print(sameNetwork('192.168.1.0/8', '192.32.45.7/8'))  
  
    # Different Network  
    print(sameNetwork('17.53.128.0/20', '17.53.127.0/20'))
  

chevron_right


Output :

Same Network
Different Network
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.