Python Script : Calculate All IPs in given CIDR notation / IP Range

Dear Friends,

Some times we need to calculate all IPs in CIDR notation like 192.168.1.1/27.

Below is the Python Script to do so:

from netaddr import IPNetwork
ipc = raw_input('Enter The IP Range ')
n = 0
#for ip in IPNetwork(ipc):
for ip in list(IPNetwork(ipc))[1:-1]:
    n = n + 1
    print '%s' % ip
print 'Total No of IPs are ' + str(n)
raw_input()


------------------------------------------------------------------------------------

Note:

1. netaddr is not available in standard package, so you need to install it using easy_install. 

2. This script removes first and last IP of the range, as they are reserved for network and broadcast address respectively.



Comments