h0arry / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

Feature request: blob format ('10.0.0.0/23' --> '10.0.0-1.*') #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Taken from 
http://stackoverflow.com/questions/6915999/python-convert-list-of-ip-cidr-string
s-to-a-human-readable-glob

In netaddr module (https://github.com/drkjam/netaddr) there are functions that 
convert either an ip range (iprange_to_globs(start, end)), or a CIDR 
(cidr_to_glob(cidr)) to a glob. A glob is a "user friendly form of specifying 
IP address ranges".

>>> import netaddr
>>> ip1 = netaddr.IPNetwork('10.0.0.0/23')
>>> netaddr.cidr_to_glob(ip1)
'10.0.0-1.*'

Also requested, the ability to convert and merge multiple ip address ranges or 
CIDRs into the least amount of globs as possible.

Example 1:

>>> list_of_ips = ['10.0.0.0/23', '10.0.2.0/24', '10.0.3.0/24']
>>> print glob_list(list_of_ips)
'10.0.0-3.*'
Example 2:

>>> list_of_ips2 = ['10.0.0.0/23', '10.0.3.0/24', '10.0.4.0/24']
>>> print glob_list(list_of_ips2)
'10.0.0-1.*, 10.0.3-4.*'
Example 3:

>>> list_of_ips3 = ['10.0.0.0', '10.0.0.3', '10.0.0.4']
>>> print glob_list(list_of_ips2)
'10.0.0.0, 10.0.0.3-4'

Original issue reported on code.google.com by ghe...@gmail.com on 2 Aug 2011 at 7:54

GoogleCodeExporter commented 9 years ago
globbing addresses like this seems like it's definitely outside of the scope of 
ipaddr. We actually had some functionality similar to this a *long* time ago 
and I pulled it out.

Original comment by pmo...@google.com on 24 Nov 2011 at 10:18