| 7 | # list of: |
| 8 | # 1.2.3.4/5 or just 1.2.3.4 |
| 9 | def 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 |