source: genesis/nodes/getrange.py@ 8119

Last change on this file since 8119 was 7972, checked in by rick, 15 years ago

Basic ErrorHandling and raise condition allowance and more readable usage warning

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 1.7 KB
Line 
1#!/usr/bin/env python
2# zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink
3# range
4# lodewijk@wirelessleiden.nl
5import re
6import sys
7import glob
8
9size = 30
10if len(sys.argv) < 2:
11 print "Gebruik: python getrange.py <interlink|subnet> <size>"
12 print "\tdus voor een /28 interlink is het 'python getrange.py interlink 28'"
13 print "\ten voor een /25 subnet is het 'python getrange.py subnet 25'"
14 exit(1)
15
16interlink = sys.argv[1] == 'interlink'
17size = int(sys.argv[2])
18numaddrs = 1 << (32 - size)
19
20substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';")
21
22def parseaddr(s):
23 f = s.split('.')
24 return (long(f[0]) << 24L) + \
25 (long(f[1]) << 16L) + \
26 (long(f[2]) << 8L) + \
27 long(f[3])
28
29def showaddr(a):
30 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
31
32bezet = {}
33
34for fname in glob.glob("*/wleiden.conf"):
35 substs = {}
36 lineNr = 0
37 for l in open(fname):
38 lineNr += 1
39 l = l[:-1]
40 match = substre.match(l)
41 if match != None:
42 substs[match.group(1)] = match.group(2)
43 if l.startswith("IP="):
44 right = l[3:]
45 if '/' not in right:
46 addr = right
47 mask = 32
48 else:
49 addr, mask = right.split('/')
50 if addr in substs:
51 addr = substs[addr]
52 try:
53 addr = parseaddr(addr)
54 mask = int(mask)
55 addr = addr & ~((1 << (32 - mask)) - 1)
56 bezet[addr] = 1
57 except ValueError, e:
58 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l)
59
60i = 0xffffffffL
61for k in bezet.keys():
62 if k < i:
63 i = k
64
65
66i = 0xac100300
67if not interlink:
68 i = 0xac110000
69while bezet.has_key(i):
70 i = i + numaddrs
71
72print "%s/%d:" % (showaddr(i), size),
73print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
Note: See TracBrowser for help on using the repository browser.