Python package – “ipaddress”

IP Address (Internet Protocol) is a fundamental networking concept that provides address assignation capability in a network. The python module ipaddress is used extensively to validate and categorize IP address to IPV4 and IPV6 type. It can also be used to do comparison of the IP address values as well as IP address arithmetic for manipulating the ip addresses.

Validating the entered input IP address

import ipaddress

IP_ADDRESS = input("Enter IP(IPv4/IPv6) Address : ")
try:
    network = ipaddress.ip_address(IP_ADDRESS)
except ValueError:
    print('Entered IP Address is invalid:', IP_ADDRESS)
else:
    print('Entered IP Address is valid:' + IP_ADDRESS)
    print(network)
$ python ip_address.py
Entered IP Address is valid: 1.1.1.1
1.1.1.1/32

$ python ip_address.py
Entered IP Address is invalid:1.1.1.256

The ip_address function validates the IPv4 address. If the range of values is beyond 0 to 255, then it throws an error i.e. “ValueError”.

Validating the entered Subnet and its details

The program takes an IP address and a subnet mask as input and gives the following information about the inputs:

  • Validating Subnet
  • Number of available hosts.
  • Network Address
  • Broadcast Address
  • Number of valid hosts per subnet
# Validating the entered subnet & Calculating multiple parameters associated in a Given Subnet
IP_SUBNET = input("Enter IP(IPv4/IPv6) subnet : ")
try:
    network = ipaddress.ip_network(IP_SUBNET)
except ValueError:
    print('Entered IP Address is invalid:', IP_SUBNET)
else:
    print('Entered IP Address is valid:' + IP_SUBNET)
    print('Number of Hosts available in this Subnet' + IP_SUBNET + ':' + str(network.num_addresses))
    print('Host List: ' + str(list(network.hosts())))
    print('Network Address: ' + str(network.network_address))
    print('Broadcast Address: ' + str(network.broadcast_address))
    print('Subnet Mask: ' + str(network.netmask))
    print('Subnet Mask Bit : ' + str(network.prefixlen))
$ python ip_address.py
Enter IP(IPv4/IPv6) subnet : 192.168.1.0/29
Entered IP Address is valid:192.168.1.0/29
Number of Hosts available in this Subnet192.168.1.0/29:8
Host List: [IPv4Address('192.168.1.1'), IPv4Address('192.168.1.2'), IPv4Address('192.168.1.3'), IPv4Address('192.168.1.4'), IPv4Address('192.168.1.5'), IPv4A
ddress('192.168.1.6')]
Network Address: 192.168.1.0
Broadcast Address: 192.168.1.7
Subnet Mask: 255.255.255.248
Subnet Mask Bit : 29

$ python ip_address.py
Enter IP(IPv4/IPv6) subnet : 192.168.1.1/29
Entered IP Address is invalid: 192.168.1.1/29

$ python ip_address.py
Enter IP(IPv4/IPv6) subnet : 192.168.1.0 /29
Entered IP Address is invalid: 192.168.1.0 /29

Last two did not execute because subnet entered was “192.168.1.1/29” which is actually not representing subnet and is actually a host. In the latter, we have actually added an space.

# Lets understand the purpose of "ipaddress" package
# Primary Goals
#       1. Validating IP Address/IP Subnet
#       2. Determining Host Address, Network Address, Broadcast Address
# Our objective is to create a Python program name "ip_address" which accept these inputs
#       1. IP_ADDRESS :
#       2. IP_SUBNET :

import ipaddress

# Validating entered IP Address
IP_ADDRESS = input("Enter IP(IPv4/IPv6) Address : ")
try:
    network = ipaddress.ip_address(IP_ADDRESS)
except ValueError:
    print('Entered IP Address is invalid:', IP_ADDRESS)
else:
    print('Entered IP Address is valid:' + IP_ADDRESS)

# Validating the entered subnet & Calculating multiple parameters associated in a Given Subnet
IP_SUBNET = input("Enter IP(IPv4/IPv6) subnet : ")
try:
    network = ipaddress.ip_network(IP_SUBNET)
except ValueError:
    print('Entered IP Address is invalid:', IP_SUBNET)
else:
    print('Entered IP Address is valid:' + IP_SUBNET)
    print('Number of Hosts available in this Subnet' + IP_SUBNET + ':' + str(network.num_addresses))
    print('Host List: ' + str(list(network.hosts())))
    print('Network Address: ' + str(network.network_address))
    print('Broadcast Address: ' + str(network.broadcast_address))
    print('Subnet Mask: ' + str(network.netmask))
    print('Subnet Mask Bit : ' + str(network.prefixlen))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.