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 192.168.0.0/24, it requires starting and ending IP addresses in a range like 192.168.0.1-192.168.0.254. 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 192.168.0.1-192.168.0.254, 10.128.0.1-10.128.0.254…
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('192.168.0.0/24') >>> print subnet.host_first <bound method Network.host_first of Network('192.168.0.0/24')> >>> print str(subnet.host_first) <bound method Network.host_first of Network('192.168.0.0/24')> >>> print(str(subnet.host_first())) 192.168.0.1 >>> print str(subnet.host_first()) 192.168.0.1 >>> print str(subnet.host_last()) 192.168.0.254 >>> print str(subnet.broadcast()) 192.168.0.255 >>> print str(subnet.info()) PRIVATE >>> print str(subnet.to_ipv6()) 2002:c0a8:0000:0000:0000:0000:0000:0000 >>> print str(subnet.size()) 256 >>> ipaddress = ipcalc.IP('192.168.0.128/24') >>> print str(ipaddress.subnet()) 24 >>> print str(ipaddress.info()) PRIVATE >>> print str(ipaddress.guess_network()) 192.168.0.0/24 >>>
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!