473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Calculating CIDR blocks

# I have list of free IP addresses. One IP per line. Now I'm trying to
# share them to CIDR blocks /24 - /32. For example: if 1.1.1.0 -
1.1.1.255
# is free, it returns list['1.1.1.0/24'] but let's say that 1.1.1.1 is
# used, then function should return list['1.1.1.128/25',
# '1.1.1.127/26', '1.1.1.32/27', etc until free block doesn't fit].

import string
import network
# http://cheeseshop.python.org/pypi/Adytum-NetCIDR/0.0.1

ips = ['192.168.0.0', '192.168.0.1', '192.168.0.2', '192.168.0.3',
'192.168.0.4', '192.168.0.5', '192.168.0.6', '192.168.0.7',
'192.168.0.8', '192.168.0.9', '192.168.0.10', '192.168.0.11',
'192.168.0.12', '192.168.0.13', '192.168.0.14', '192.168.0.15',
'192.168.0.16', '192.168.0.17', '192.168.0.18', '192.168.0.19',
'192.168.0.20', '192.168.0.21', '192.168.0.22', '192.168.0.23',
'192.168.0.24', '192.168.0.25', '192.168.0.26', '192.168.0.27',
'192.168.0.28', '192.168.0.29', '192.168.0.30', '192.168.0.31',
'192.168.0.32', '192.168.0.33', '192.168.0.34', '192.168.0.35',
'192.168.0.36', '192.168.0.37', '192.168.0.38', '192.168.0.39',
'192.168.0.40', '192.168.0.41', '192.168.0.42', '192.168.0.43',
'192.168.0.44', '192.168.0.45', '192.168.0.46', '192.168.0.47',
'192.168.0.48', '192.168.0.49', '192.168.0.50', '192.168.0.51',
'192.168.0.52', '192.168.0.53', '192.168.0.54', '192.168.0.55',
'192.168.0.56', '192.168.0.57', '192.168.0.58', '192.168.0.59',
'192.168.0.60', '192.168.0.61', '192.168.0.62', '192.168.0.63',
'192.168.0.64', '192.168.0.65', '192.168.0.66', '192.168.0.67',
'192.168.0.68', '192.168.0.69', '192.168.0.70', '192.168.0.71',
'192.168.0.72', '192.168.0.73', '192.168.0.74', '192.168.0.75',
'192.168.0.76', '192.168.0.77', '192.168.0.78', '192.168.0.79',
'192.168.0.80', '192.168.0.81', '192.168.0.82', '192.168.0.83',
'192.168.0.84', '192.168.0.85', '192.168.0.86', '192.168.0.87',
'192.168.0.88', '192.168.0.89', '192.168.0.90', '192.168.0.91',
'192.168.0.92', '192.168.0.93', '192.168.0.94', '192.168.0.95',
'192.168.0.96', '192.168.0.97', '192.168.0.98','192.168.0.99',
'192.168.0.100', '192.168.0.101', '192.168.0.102', '192.168.0.103',
'192.168.0.104', '192.168.0.105', '192.168.0.106', '192.168.0.107',
'192.168.0.108', '192.168.0.109', '192.168.0.110', '192.168.0.112',
'192.168.0.113', '192.168.0.114', '192.168.0.115', '192.168.0.116',
'192.168.0.117', '192.168.0.118', '192.168.0.119', '192.168.0.120',
'192.168.0.121', '192.168.0.122', '192.168.0.123', '192.168.0.124',
'192.168.0.125', '192.168.0.126', '192.168.0.127', '192.168.0.128',
'192.168.0.129', '192.168.0.130', '192.168.0.131', '192.168.0.132',
'192.168.0.133', '192.168.0.134', '192.168.0.135', '192.168.0.136',
'192.168.0.137', '192.168.0.138', '192.168.0.139', '192.168.0.140',
'192.168.0.141', '192.168.0.142', '192.168.0.143', '192.168.0.144',
'192.168.0.145', '192.168.0.146', '192.168.0.147', '192.168.0.148',
'192.168.0.149', '192.168.0.150', '192.168.0.151', '192.168.0.152',
'192.168.0.153', '192.168.0.154', '192.168.0.155', '192.168.0.156',
'192.168.0.157', '192.168.0.158', '192.168.0.159', '192.168.0.160',
'192.168.0.161', '192.168.0.162', '192.168.0.163', '192.168.0.164',
'192.168.0.165', '192.168.0.166', '192.168.0.167', '192.168.0.168',
'192.168.0.169', '192.168.0.170', '192.168.0.171', '192.168.0.172',
'192.168.0.173', '192.168.0.174', '192.168.0.175', '192.168.0.176',
'192.168.0.177', '192.168.0.178', '192.168.0.179', '192.168.0.180',
'192.168.0.181', '192.168.0.182', '192.168.0.183', '192.168.0.184',
'192.168.0.185', '192.168.0.186', '192.168.0.187', '192.168.0.188',
'192.168.0.189', '192.168.0.190', '192.168.0.191', '192.168.0.192',
'192.168.0.193', '192.168.0.194', '192.168.0.195', '192.168.0.196',
'192.168.0.197', '192.168.0.198', '192.168.0.199', '192.168.0.200',
'192.168.0.201', '192.168.0.202', '192.168.0.203', '192.168.0.204',
'192.168.0.205', '192.168.0.206', '192.168.0.207', '192.168.0.208',
'192.168.0.209', '192.168.0.210', '192.168.0.211', '192.168.0.212',
'192.168.0.213', '192.168.0.214', '192.168.0.215', '192.168.0.216',
'192.168.0.217', '192.168.0.218', '192.168.0.219', '192.168.0.220',
'192.168.0.221', '192.168.0.222', '192.168.0.223', '192.168.0.224',
'192.168.0.225', '192.168.0.226', '192.168.0.227', '192.168.0.228',
'192.168.0.229', '192.168.0.230', '192.168.0.231', '192.168.0.232',
'192.168.0.233', '192.168.0.234', '192.168.0.235', '192.168.0.236',
'192.168.0.237', '192.168.0.238', '192.168.0.239', '192.168.0.240',
'192.168.0.241', '192.168.0.242', '192.168.0.243', '192.168.0.244',
'192.168.0.245', '192.168.0.246', '192.168.0.247', '192.168.0.248',
'192.168.0.249', '192.168.0.250', '192.168.0.251', '192.168.0.252',
'192.168.0.253', '192.168.0.254', '192.168.0.255']
# note: 192.168.0.111 is missing
cidrs = [24, 25, 26, 27, 28, 29, 30, 31, 32]

free = []

for cidr in cidrs:
c = 0
for ip in ips:
ip_addr = network.CIDR("%s/32" % ip)
my_network = "%s/%d" % (ip, cidr)
network_CIDR = network.CIDR(my_network)
myn = network.Networks([network_CIDR])

freelist = []

if len(free) 0:
for i in free:
freelist.append(network.CIDR("%s" % i))

freeh = network.Networks(freelist)

net_len = network_CIDR.getHostCount()

if ip_addr in myn and ip_addr not in freeh:
c = c + 1
if c == net_len:
hr = network.CIDR(my_network).getHostRange()
free.append("%s/%s" % (hr[0], cidr))
c = 0

print free

# Outputs:
# ['192.168.0.128/25', '192.168.0.64/26', '192.168.0.32/27',
'192.168.0.16/28', '192.168.0.8/29', '192.168.0.4/30']
# That 192.168.0.64/26 cannot be there because it's 192.168.0.64 -
192.168.0.127 and 192.168.0.111 does not exist in ips list!
# Any ideas?

Apr 20 '07 #1
5 5083
On 20 huhti, 04:32, Pekka Järvinen <pekka.jarvi...@gmail.comwrote:
# I have list of free IP addresses. One IP per line. Now I'm trying to
# share them to CIDR blocks /24 - /32. For example: if 1.1.1.0 -
1.1.1.255
# is free, it returns list['1.1.1.0/24'] but let's say that 1.1.1.1 is
# used, then function should return list['1.1.1.128/25',
# '1.1.1.127/26', '1.1.1.32/27', etc until free block doesn't fit].

import string
import network
#http://cheeseshop.python.org/pypi/Adytum-NetCIDR/0.0.1

ips = ['192.168.0.0', '192.168.0.1', '192.168.0.2', '192.168.0.3',
'192.168.0.4', '192.168.0.5', '192.168.0.6', '192.168.0.7',
'192.168.0.8', '192.168.0.9', '192.168.0.10', '192.168.0.11',
'192.168.0.12', '192.168.0.13', '192.168.0.14', '192.168.0.15',
'192.168.0.16', '192.168.0.17', '192.168.0.18', '192.168.0.19',
'192.168.0.20', '192.168.0.21', '192.168.0.22', '192.168.0.23',
'192.168.0.24', '192.168.0.25', '192.168.0.26', '192.168.0.27',
'192.168.0.28', '192.168.0.29', '192.168.0.30', '192.168.0.31',
'192.168.0.32', '192.168.0.33', '192.168.0.34', '192.168.0.35',
'192.168.0.36', '192.168.0.37', '192.168.0.38', '192.168.0.39',
'192.168.0.40', '192.168.0.41', '192.168.0.42', '192.168.0.43',
'192.168.0.44', '192.168.0.45', '192.168.0.46', '192.168.0.47',
'192.168.0.48', '192.168.0.49', '192.168.0.50', '192.168.0.51',
'192.168.0.52', '192.168.0.53', '192.168.0.54', '192.168.0.55',
'192.168.0.56', '192.168.0.57', '192.168.0.58', '192.168.0.59',
'192.168.0.60', '192.168.0.61', '192.168.0.62', '192.168.0.63',
'192.168.0.64', '192.168.0.65', '192.168.0.66', '192.168.0.67',
'192.168.0.68', '192.168.0.69', '192.168.0.70', '192.168.0.71',
'192.168.0.72', '192.168.0.73', '192.168.0.74', '192.168.0.75',
'192.168.0.76', '192.168.0.77', '192.168.0.78', '192.168.0.79',
'192.168.0.80', '192.168.0.81', '192.168.0.82', '192.168.0.83',
'192.168.0.84', '192.168.0.85', '192.168.0.86', '192.168.0.87',
'192.168.0.88', '192.168.0.89', '192.168.0.90', '192.168.0.91',
'192.168.0.92', '192.168.0.93', '192.168.0.94', '192.168.0.95',
'192.168.0.96', '192.168.0.97', '192.168.0.98','192.168.0.99',
'192.168.0.100', '192.168.0.101', '192.168.0.102', '192.168.0.103',
'192.168.0.104', '192.168.0.105', '192.168.0.106', '192.168.0.107',
'192.168.0.108', '192.168.0.109', '192.168.0.110', '192.168.0.112',
'192.168.0.113', '192.168.0.114', '192.168.0.115', '192.168.0.116',
'192.168.0.117', '192.168.0.118', '192.168.0.119', '192.168.0.120',
'192.168.0.121', '192.168.0.122', '192.168.0.123', '192.168.0.124',
'192.168.0.125', '192.168.0.126', '192.168.0.127', '192.168.0.128',
'192.168.0.129', '192.168.0.130', '192.168.0.131', '192.168.0.132',
'192.168.0.133', '192.168.0.134', '192.168.0.135', '192.168.0.136',
'192.168.0.137', '192.168.0.138', '192.168.0.139', '192.168.0.140',
'192.168.0.141', '192.168.0.142', '192.168.0.143', '192.168.0.144',
'192.168.0.145', '192.168.0.146', '192.168.0.147', '192.168.0.148',
'192.168.0.149', '192.168.0.150', '192.168.0.151', '192.168.0.152',
'192.168.0.153', '192.168.0.154', '192.168.0.155', '192.168.0.156',
'192.168.0.157', '192.168.0.158', '192.168.0.159', '192.168.0.160',
'192.168.0.161', '192.168.0.162', '192.168.0.163', '192.168.0.164',
'192.168.0.165', '192.168.0.166', '192.168.0.167', '192.168.0.168',
'192.168.0.169', '192.168.0.170', '192.168.0.171', '192.168.0.172',
'192.168.0.173', '192.168.0.174', '192.168.0.175', '192.168.0.176',
'192.168.0.177', '192.168.0.178', '192.168.0.179', '192.168.0.180',
'192.168.0.181', '192.168.0.182', '192.168.0.183', '192.168.0.184',
'192.168.0.185', '192.168.0.186', '192.168.0.187', '192.168.0.188',
'192.168.0.189', '192.168.0.190', '192.168.0.191', '192.168.0.192',
'192.168.0.193', '192.168.0.194', '192.168.0.195', '192.168.0.196',
'192.168.0.197', '192.168.0.198', '192.168.0.199', '192.168.0.200',
'192.168.0.201', '192.168.0.202', '192.168.0.203', '192.168.0.204',
'192.168.0.205', '192.168.0.206', '192.168.0.207', '192.168.0.208',
'192.168.0.209', '192.168.0.210', '192.168.0.211', '192.168.0.212',
'192.168.0.213', '192.168.0.214', '192.168.0.215', '192.168.0.216',
'192.168.0.217', '192.168.0.218', '192.168.0.219', '192.168.0.220',
'192.168.0.221', '192.168.0.222', '192.168.0.223', '192.168.0.224',
'192.168.0.225', '192.168.0.226', '192.168.0.227', '192.168.0.228',
'192.168.0.229', '192.168.0.230', '192.168.0.231', '192.168.0.232',
'192.168.0.233', '192.168.0.234', '192.168.0.235', '192.168.0.236',
'192.168.0.237', '192.168.0.238', '192.168.0.239', '192.168.0.240',
'192.168.0.241', '192.168.0.242', '192.168.0.243', '192.168.0.244',
'192.168.0.245', '192.168.0.246', '192.168.0.247', '192.168.0.248',
'192.168.0.249', '192.168.0.250', '192.168.0.251', '192.168.0.252',
'192.168.0.253', '192.168.0.254', '192.168.0.255']
# note: 192.168.0.111 is missing

cidrs = [24, 25, 26, 27, 28, 29, 30, 31, 32]

free = []

for cidr in cidrs:
c = 0
for ip in ips:
ip_addr = network.CIDR("%s/32" % ip)
my_network = "%s/%d" % (ip, cidr)
network_CIDR = network.CIDR(my_network)
myn = network.Networks([network_CIDR])

freelist = []

if len(free) 0:
for i in free:
freelist.append(network.CIDR("%s" % i))

freeh = network.Networks(freelist)

net_len = network_CIDR.getHostCount()

if ip_addr in myn and ip_addr not in freeh:
c = c + 1
if c == net_len:
hr = network.CIDR(my_network).getHostRange()
free.append("%s/%s" % (hr[0], cidr))
c = 0

print free

# Outputs:
# ['192.168.0.128/25', '192.168.0.64/26', '192.168.0.32/27',
'192.168.0.16/28', '192.168.0.8/29', '192.168.0.4/30']
# That 192.168.0.64/26 cannot be there because it's 192.168.0.64 -
192.168.0.127 and 192.168.0.111 does not exist in ips list!
# Any ideas?
Little changes:

for cidr in cidrs:
c = 0
for ip in ips:
ip_addr = network.CIDR("%s/32" % ip)
my_network = "%s/%d" % (ip, cidr)
network_CIDR = network.CIDR(my_network)
myn = network.Networks([network_CIDR])

freelist = []

if len(free) 0:
for i in free:
freelist.append(network.CIDR("%s" % i))

freeh = network.Networks(freelist)

net_len = network_CIDR.getHostCount()

if ((network_CIDR in myn) and (network_CIDR not in freeh)):
c = c + 1
if ((c + 2) == net_len):
hr = network.CIDR(my_network).getHostRange()
free.append("%s/%s" % (hr[0], cidr))
c = 0

print free

now outputs:
['192.168.0.128/25', '192.168.0.0/26', '192.168.0.64/27',
'192.168.0.96/28', '192.168.0.112/28']

Which is much better, but now 192.168.0.96/28 includes that
192.168.0.111, so no go. 192.168.0.96/28 must be split into smaller
pieces still.

Apr 20 '07 #2
Look at:
http://aspn.activestate.com/ASPN/Coo.../Recipe/466298
it handles most of the logic of combining IP ranges.

Eddie
Apr 20 '07 #3
On 20 huhti, 14:34, e...@holyrood.ed.ac.uk (Eddie Corns) wrote:
Look at:http://aspn.activestate.com/ASPN/Coo.../Recipe/466298
it handles most of the logic of combining IP ranges.

Eddie
I'm getting error:
Traceback (most recent call last):
File "ipr.py", line 5, in <module>
x = ipv4.IP4Range(ips)
File "IP4Range.py", line 119, in __init__
raise TypeError("Invalid argument.")
TypeError: Invalid argument.

Apr 20 '07 #4
=?iso-8859-1?q?Pekka_J=E4rvinen?= <pe************@gmail.comwrites:
>On 20 huhti, 14:34, e...@holyrood.ed.ac.uk (Eddie Corns) wrote:
>Look at:http://aspn.activestate.com/ASPN/Coo.../Recipe/466298
it handles most of the logic of combining IP ranges.

Eddie
>I'm getting error:
Traceback (most recent call last):
File "ipr.py", line 5, in <module>
x = ipv4.IP4Range(ips)
File "IP4Range.py", line 119, in __init__
raise TypeError("Invalid argument.")
TypeError: Invalid argument.
I haven't used this module for some time but if I recall your requirements
properly you need something like:
>>R=IP4.IP4Range()
for i in ips:
.... R=R|IP4.IP4Range(i)
....
>>for m in R.itermasks():
.... print m
....
192.168.0.0/26
192.168.0.64/27
192.168.0.96/29
192.168.0.104/30
192.168.0.108/31
192.168.0.110/32
192.168.0.128/25
192.168.0.112/28
>>>
There may be an easier way my memory is hazy.

Eddie
Apr 20 '07 #5
On 20 huhti, 18:30, e...@holyrood.ed.ac.uk (Eddie Corns) wrote:
=?iso-8859-1?q?Pekka_J=E4rvinen?= <pekka.jarvi...@gmail.comwrites:
On 20 huhti, 14:34, e...@holyrood.ed.ac.uk (Eddie Corns) wrote:
Look at:http://aspn.activestate.com/ASPN/Coo.../Recipe/466298
it handles most of the logic of combining IP ranges.
Eddie
I'm getting error:
Traceback (most recent call last):
File "ipr.py", line 5, in <module>
x = ipv4.IP4Range(ips)
File "IP4Range.py", line 119, in __init__
raise TypeError("Invalid argument.")
TypeError: Invalid argument.

I haven't used this module for some time but if I recall your requirements
properly you need something like:
>R=IP4.IP4Range()
for i in ips:

... R=R|IP4.IP4Range(i)
...>>for m in R.itermasks():

... print m
...
192.168.0.0/26
192.168.0.64/27
192.168.0.96/29
192.168.0.104/30
192.168.0.108/31
192.168.0.110/32
192.168.0.128/25
192.168.0.112/28

There may be an easier way my memory is hazy.

Eddie
It does exactly what I'm after! Thanks a lot! :)

Apr 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
0
by: Kasp | last post by:
Hi there, I am trying to make an OLAP cube on a table having two columns (datetime, Number_of_times_an_event_occured). My dimension is time and I want to measure the Min and Max times an event...
1
by: mpayne | last post by:
I have a span that has a width of 25%. I want to use that span as the top of a dialog box that has rounded corners. So, the span itself is broken up into 3 smaller spans, one for the left and...
4
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an...
11
by: 193.123.19.116 [Daz] | last post by:
Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)? ================================== Poster's IP...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
0
by: pekka.jarvinen | last post by:
Hi, I have array of free IP addresses. One IP per line. Now I'm trying to share them to CIDR blocks /24 - /32. For example: if 1.1.1.0-1.1.1.255 is free, it returns array('1.1.1.0/24') but let's...
2
ashitpro
by: ashitpro | last post by:
I want to check whether given IP exist in CIDR range.. e.g. IP is 192.168.1.3 CIDR is 192.168.1.0/24 here according to CIDR lower ip is 192.168.1.1 higher ip is 192.168.1.254
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.