1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#!/usr/bin/env python
# Copyright (C) 2012 Peter Hatina <phatina@redhat.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import re
import sys
import itertools
class CuraIpv4Addr:
class CuraIpv4Member:
def __init__(self, member):
self.m_member = member
def enumerate(self):
pattern = re.compile("^{(.*)}$")
match = pattern.search(self.m_member)
member = match.group(1) if match else []
if not member:
return [self.m_member]
ranges = (x.split("-") for x in member.split(";"))
try:
members = []
for r in ranges:
if not 0 <= int(r[0]) <= 255 or not 0 <= int(r[-1]) <= 255:
return []
low = int(r[0])
high = int(r[-1])
if low > high:
low, high = high, low
members += [str(i) for i in range(low, high + 1)]
except ValueError:
return []
return members
def __init__(self, addr_pattern):
self.m_addr_pattern = addr_pattern
def enumerate(self):
str_members = self.m_addr_pattern.split(".")
if len(str_members) != 4:
return []
members = []
for m in str_members:
ipm = CuraIpv4Addr.CuraIpv4Member(m)
members.append(ipm)
addresses = members[3].enumerate()
if not addresses:
return []
for i in reversed(range(0, 3)):
combined = []
members_to_combine = members[i].enumerate()
if not members_to_combine:
return []
for r in itertools.product(members_to_combine, addresses):
combined.append(".".join([r[0], r[1]]))
addresses = combined
return sorted(set(addresses))
class CuraIpv4AddrGenerator:
def __init__(self, addresses):
self.m_addresses = addresses
def enumerate(self):
rval = []
for addr in self.m_addresses.split(","):
rval += CuraIpv4Addr(addr).enumerate()
return rval
|