MCPcopy Create free account
hub / github.com/apenwarr/sshuttle / parse_subnets

Function parse_subnets

main.py:9–26  ·  view source on GitHub ↗
(subnets_str)

Source from the content-addressed store, hash-verified

7# list of:
8# 1.2.3.4/5 or just 1.2.3.4
9def parse_subnets(subnets_str):
10 subnets = []
11 for s in subnets_str:
12 m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
13 if not m:
14 raise Fatal('%r is not a valid IP subnet format' % s)
15 (a,b,c,d,width) = m.groups()
16 (a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
17 if width == None:
18 width = 32
19 else:
20 width = int(width)
21 if a > 255 or b > 255 or c > 255 or d > 255:
22 raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
23 if width > 32:
24 raise Fatal('*/%d is greater than the maximum of 32' % width)
25 subnets.append(('%d.%d.%d.%d' % (a,b,c,d), width))
26 return subnets
27
28
29# 1.2.3.4:567 or just 1.2.3.4 or just 567

Callers 1

main.pyFile · 0.85

Calls 1

FatalClass · 0.85

Tested by

no test coverage detected