Using Python for IP addresses

Part of the day to day tasks of any Information Security professional may include transforming system input and output text and calculating IP addresses. In this article I demonstrate how use Python to work with IP addresses and transform system input/output.

A system that I manage at work doesn’t allow you to enter networks in CIDR notation like, it requires starting and ending IP addresses in a range like In the past I’ve used ipcalc on Linux to calculate IP addresses when I have something other than a simple netmask to calculate. Today I had a list of IP subnets of various netmasks that I needed to transform into a comma separated list in the format of,…

I’m going to use the ipcalc module for Python to demonstrate how to do this. You can install it from an administrator cmd prompt or using root or sudo in a Linux terminal with the following command:

pip install ipcalc

Pretty simple to install. Now lets move on to a demonstration of how to use it. After importing the ipcalc module, I created a new object and attempted to output the first and last host on the network but it didn’t work quite as expected until I cast the output to string.

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import ipcalc
>>> subnet = ipcalc.Network('')
>>> print subnet.host_first
<bound method Network.host_first of Network('')>
>>> print str(subnet.host_first)
<bound method Network.host_first of Network('')>
>>> print(str(subnet.host_first()))
>>> print str(subnet.host_first())
>>> print str(subnet.host_last())
>>> print str(subnet.broadcast())
>>> print str(
>>> print str(subnet.to_ipv6())
>>> print str(subnet.size())
>>> ipaddress = ipcalc.IP('')
>>> print str(ipaddress.subnet())
>>> print str(
>>> print str(ipaddress.guess_network())

Now I’m going to take an input file with a network name followed by network address in CIDR format on each line, cut out just the subnet, and output the networks in firsthost-lasthost,firsthost-lasthost,…” format.

>>> with open('ips.txt') as f:
    content = f.readlines()

>>> content = [x.strip() for x in content]

>>> import sys
>>> for x in content:
    subnet = x.split(' ')[-1]
    networkobj = ipcalc.Network(subnet)
    sys.stdout.write("%s-%s," % (str(networkobj.host_first()),str(networkobj.host_last())))

In the code above, I loop through each line of the file and get the last word in the string which is the subnet address. It was necessary to use sys.stdout.write() to get rid of the space between each network start and end addresses using Python 2.7. I’m not going to show you the output. If you want to know my public IP addresses then you’ll have to work to find them yourself. 🙂

I’m sure that there are more than one way to write the code for this exercise. If you have any constructive feedback please leave a comment. Thanks for visiting!

(Visited 265 times, 6 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *