Home > Net >  Why this code does not identify the class of an IP in some cases?
Why this code does not identify the class of an IP in some cases?

Time:01-01

I wrote the following Python code that is mean to classify whether an IP is valid, if valid then to what class it belongs and whether it is private:

from ipaddress import IPv4Address, IPv4Network, ip_address
import sys

try:
    ip = ip_address(sys.argv[1])
    print(f"{ip} is a correct IP{ip.version} address.")
    if ip_address(ip).is_private:
            print("It is a private IP.")
    classA = IPv4Network(("10.0.0.0", "255.0.0.0")) 
    classB = IPv4Network(("172.16.0.0", "255.240.0.0"))
    classC = IPv4Network(("192.168.0.0", "255.255.0.0")) 
    if ip in classA:
        print(f"{ip} is a correct class A IP{ip.version} address.")
        if ip_address(ip).is_private:
            print("It is a private IP.")
    elif ip in classB:
        print(f"{ip} is a correct class B IP{ip.version} address.")
        if ip_address(ip).is_private:
            print("It is a private IP.")
    elif ip in classC:
        print(f"{ip} is a correct class C IP{ip.version} address.") 
        if ip_address(ip).is_private:
            print("It is a private IP.")
except ValueError:
    print(f'address/netmask is invalid: {sys.argv[1]}')
except:
    print(f'Usage : {sys.argv[0]}  ip')

I assumed that the range of class A is from 1.0.0.0 to 127.255.255.255, of class B is from 128.0.0.0 to 191.255.255.255, and of class C is from 192.0.0.0 to 223.255.255.255. When given 141.68.27.102 it does not classify the class as B (although the answer to this cisco lab does), why so? When given 192.12.13.14 it does not classify the class as C (although the answer to this cisco lab does), why so?

CodePudding user response:

The short answer is that "because those IPs don't belong in the classes as you've defined them".

This somewhat simplified rework of your program prints out the classes first, so you can see how you've defined them... and why they don't seem to properly match.

from ipaddress import IPv4Network, ip_address

classes = {
    "A": IPv4Network(("10.0.0.0", "255.0.0.0")),
    "B": IPv4Network(("172.16.0.0", "255.240.0.0")),
    "C": IPv4Network(("192.168.0.0", "255.255.0.0")),
}


def describe_ip(ip_str):
    ip = ip_address(ip_str)
    for class_name, network in classes.items():
        if ip in network:
            print(f"{ip} is a correct class {class_name} IP{ip.version} address.")
            break
    else:
        print(f"{ip} does not belong in any of the classes defined.")


print(classes)
describe_ip("172.32.255.255")
describe_ip("141.68.27.102")
describe_ip("192.12.13.14")
describe_ip("172.16.255.255")

The output is something like (where the last line proves that 172.16. addresses do match your B class definition).

{'A': IPv4Network('10.0.0.0/8'), 'B': IPv4Network('172.16.0.0/12'), 'C': IPv4Network('192.168.0.0/16')}
172.32.255.255 does not belong in any of the classes defined.
141.68.27.102 does not belong in any of the classes defined.
192.12.13.14 does not belong in any of the classes defined.
172.16.255.255 is a correct class B IP4 address.
  • Related