source: genesis/tools/getrange.py@ 9277

Last change on this file since 9277 was 8615, checked in by rick, 14 years ago

Bezempje door alle files. Rommel weg. Tooljes op de juiste locatie.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.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
9def parseaddr(s):
10 f = s.split('.')
11 return (long(f[0]) << 24L) + \
12 (long(f[1]) << 16L) + \
13 (long(f[2]) << 8L) + \
14 long(f[3])
15
16def showaddr(a):
17 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
18
19def get_ranges(interlink, size, aantal):
20 numaddrs = 1 << (32 - size)
21
22 substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';")
23
24
25 bezet = {}
26
27 for fname in glob.glob("*/wleiden.conf"):
28 substs = {}
29 lineNr = 0
30 for l in open(fname):
31 lineNr += 1
32 l = l[:-1]
33 match = substre.match(l)
34 if match != None:
35 substs[match.group(1)] = match.group(2)
36 if l.startswith("IP="):
37 right = l[3:]
38 if '/' not in right:
39 addr = right
40 mask = 32
41 else:
42 addr, mask = right.split('/')
43 if addr in substs:
44 addr = substs[addr]
45 try:
46 addr = parseaddr(addr)
47 mask = int(mask)
48 addr = addr & ~((1 << (32 - mask)) - 1)
49 for i in range(0, (1 << (32 - mask))):
50 bezet[addr + i] = 1
51 except ValueError, e:
52 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l)
53
54 # Fetching smallest in use IP
55 # XXX: Currently not used for anything
56 i = parseaddr("255.255.255.255")
57 for k in bezet.keys():
58 if k < i:
59 i = k
60
61 # Interlinks are living in special ranges usually
62 if interlink:
63 i = parseaddr("172.16.3.0")
64 else:
65 i = parseaddr("172.17.0.0")
66
67 ranges = []
68 for n in xrange(aantal):
69 okay = False
70 while not okay:
71 while bezet.has_key(i):
72 i = i + numaddrs
73
74 # Assume Ok, till we find a already used IP
75 okay = True
76 for j in range(numaddrs):
77 if bezet.has_key(i + j):
78 i = i + numaddrs
79 okay = False
80 break
81 ranges.append(i)
82 bezet[i] = 1
83
84 return ranges
85
86def main():
87 if len(sys.argv) < 2:
88 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
89 print "Voorbeelden:"
90 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
91 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
92 exit(1)
93
94 # Argument parsing
95 interlink = sys.argv[1] == 'interlink'
96 size = int(sys.argv[2])
97 try:
98 aantal = int(sys.argv[3])
99 except (KeyError,IndexError):
100 aantal = 1
101
102 ranges = get_ranges(interlink, size, aantal)
103 numaddrs = 1 << (32 - size)
104
105 for i in ranges:
106 print "%s/%d:" % (showaddr(i), size),
107 if size > 28:
108 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
109 else:
110 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
111 print "...",
112 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
113
114if __name__ == "__main__":
115 main()
Note: See TracBrowser for help on using the repository browser.