Calculating CIDR blocks

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Apr 20 11:30:05 EDT 2007


=?iso-8859-1?q?Pekka_J=E4rvinen?= <pekka.jarvinen at gmail.com> writes:

>On 20 huhti, 14:34, e... at holyrood.ed.ac.uk (Eddie Corns) wrote:
>> Look at:http://aspn.activestate.com/ASPN/Cookbook/Python/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



More information about the Python-list mailing list