Python ipaddr Module Secrets Revealed

As we all know, python is the language of ease and convenience, making it quite popular among programmers. Moreover, it covers a wide range of domains with a number of libraries. Today, we will discuss one such library, i.e., the python ipaddr module. This article will see how we can use the ipaddr module to work on IP addresses. So, let’s start.

ipaddr Module

ipaddr.py is the library that is used to work on IP addresses. It works on both formats of IP addresses, i.e., IPv4 and IPv6. ipaddr has mainly four classes that users opt to work on. They are as follows.

  • IPv4Address
  • IPv6Address
  • IPv4Network
  • IPv6Network

Most operations performed by a network administrator are similar for both types of IP addresses, like finding subnets, finding supernets, etc. Similarly, both addresses and networks have much in common; the process of converting a given 32 or 128 bits number into a human-readable string notation, determining the IP is within a valid range. Let’s see its installation and then will go through some examples.

Installation of Python ipaddr

pip install ipaddr

To check whether it is installed successfully or not, use the following command.

>>> import ipaddr
>>> ipaddr.__version__
'2.2.0'

Once we have installed it successfully, let’s start with some functions.

ipaddr.IPAddress()

This method is used to check the format of an IP address and whether the address we passed is of IPv4 type or IPv6 type. It takes the address as the argument and returns the object of the format.

>>> ipaddr.IPAddress('192.168.101.41')
IPv4Address('192.168.101.41')

>>> ipaddr.IPAddress('fe80::c98d:a923:aa61')
IPv6Address('fe80::c98d:a923:aa61')

ipaddr.IPNetwork()

Using this method, we can find the IP network of an IP address. To do that, we need to pass the address as the argument for the function, and it returns us an IPv4Network or IPv6Network object.

>>> ipaddr.IPNetwork('192.168.101.41')
IPv4Network('192.168.101.41/32')

>>> ipaddr.IPNetwork('fe80::c98d:a923:aa61')
IPv6Network('fe80::c98d:a923:aa61/128')

IPv4Address() and IPv6Address() Classes

These two classes are used to construct the IPv4Address and IPv6Address. We need to pass the address in any datatype as the constructor’s argument, and it returns the object of that class.

>>> ipaddr.IPv4Address('192.168.101.41')
IPv4Address('192.168.101.41')

>>> ipaddr.IPv4Address(3232261417)
IPv4Address('192.168.101.41')

>>> ipaddr.IPv4Address(b'\xc0\xa8e)')
IPv4Address('192.168.101.41')
>>> ipaddr.IPv6Address('fe80::c98d:a923:aa61')
IPv6Address('fe80::c98d:a923:aa61')

>>> ipaddr.IPv6Address(338288524927261208548678211627156242432)
IPv6Address('fe80::192d:4ada:7be5:2040:0')

>>> ipaddr.IPv6Address(b'\xfe\x80\x00\x00\x00\x00\x19-J\xda{\xe5 @\x00\x00')
IPv6Address('fe80::192d:4ada:7be5:2040:0')

Conversion of Addresses into Integer or String

Sometimes, it happens that we need our IP address in the integer format or string format. So to convert it, we will use the following lines of code.

>>> int(ipaddr.IPAddress('192.168.101.41'))
3232261417

>>> str(ipaddr.IPAddress('192.168.101.41'))
'192.168.101.41'

>>> int(ipaddr.IPAddress('fe80::c98d:a923:aa61'))
338288524927261089654019118451612953185

>>> str(ipaddr.IPAddress('fe80::c98d:a923:aa61'))
'fe80::c98d:a923:aa61'

So, in the above examples, we type-casted the object of an address into the desired datatype, which returns the values accordingly.

ipaddr.v4_int_to_packed() and ipaddr.v6_int_to_packed

These methods return the binary representation of the address we pass as the argument. We should care about here that we have to pass the IP address in integer format.

So, the integral representation of 192.168.101.41 is 3232261417, and we have to pass it as the argument. Similarly, for ‘fe80::c98d:a923:aa61’ the integral representation is 338288524927261208548678211627156242432. Let’s see the example.

>>> ipaddr.v4_int_to_packed(3232261417)
b'\xc0\xa8e)'
>>>ipaddr.v6_int_to_packed(338288524927261208548678211627156242432)
b'\xfe\x80\x00\x00\x00\x00\x19-J\xda{\xe5 @\x00\x00'

Comparing Two Addresses

We can also compare two addresses using the comparison operators in ipaddr module. The comparison will return the boolean value for the comparisons. If it returns true, it means that the comparison holds true for the given values and if false means the comparison doesn’t hold true.

>>> ipaddr.IPv4Address('127.0.0.2') > ipaddr.IPv4Address('127.0.0.1')
True
>>> ipaddr.IPv4Address('127.0.0.2') == ipaddr.IPv4Address('127.0.0.1')
False
>>> ipaddr.IPv4Address('127.0.0.2') != ipaddr.IPv4Address('127.0.0.1')
True
>>> ipaddr.IPv6Address('fe80::1234') == ipaddr.IPv6Address('fe80::1234')
True

Finding Subnets of an IPNetwork

So, we can find subnets for an IP Address using the subnet() method of the ipaddr module. Let’s see how we can do it.

>>> list(ipaddr.IPNetwork('192.0.2.0/24').subnet())
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]

In the above case, we pass the network as the argument in the IPNetwork class and then access the subnet() method from that class which returns the list of IP Address subnets.

FAQs on Python ipaddr

Q1) How do I check if an IP address is reachable in python?

We can use the following command to do that,

import nmap, socket
ip_addr = input('Enter IP or url to check if it is up or down: ')
scanner = nmap.PortScanner()
host = socket.gethostbyname(ip_addr)
scanner.scan(host, '1', '-v')
print("IP Status: ", scanner[host].state())

Q2) How do I change my IP address in python?

You need to use any VPN network service which can be controlled via python. Or use a proxy applied upon your requests to change IP address. Moreover, you can access other IP addresses in python using the addition or subtraction operator.

ip = ip + 1

Conclusion

So, today in this article, we have discussed the ipaddr module in python. We have seen different classes and methods within that module. Also, we know now how we can check the required information of an IPAddress or an IP network. We have also seen a comparison between two IP Addresses. In the end, we have seen how we can find subnets of an IP network using the module. I hope this article has helped you. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments