#

Wednesday, July 2, 2025

Working with IPAddress Library in Python for Network Engineers

Following is the official link of IPAddress library of Python.

https://docs.python.org/3/library/ipaddress.html
 
Let's install IPAddress library by installing it in Ubuntu which is my dev environment.

python3 -m pip install ipaddress

To see what it can do, I am using iPython in a Venv

To know how to use iPython in a Venv, go to end of the following post.


Now let's start by importing ipaddress library into iPython. Following example shows how the functions of IPAddress library can be used to get all the hosts in a given subnet, find the subnet mask and wild card masks which are very handy in writing Networking related scripts.
















If you want to define a variable with an IP address,

In this example, the given IP address is assigned to my_ip variable and then some functions had run to check details of it like to check it is a private IP or not multicast IP or not.




Following is just an example of using IPAddress library which checks whether 2 IPs are in the same subnet or not.

ip_network function calculates the network id of the given IP and store in a variable to do this.











No comments:

Post a Comment