source: genesis/tools/getrange.py@ 10032

Last change on this file since 10032 was 9516, checked in by rick, 13 years ago

Do not forget about the master_ip which is still used, even if not defined in interface config.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.8 KB
RevLine 
[7089]1#!/usr/bin/env python
[4150]2# zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink
3# range
[5283]4# lodewijk@wirelessleiden.nl
[3130]5import re
[4150]6import sys
[7936]7import glob
[3130]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
[8228]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)
[9516]36 if l.startswith("IP=") or l.startswith("$master_ip="):
37 if l.startswith("$master_ip="):
38 right = l.split("'")[1]
39 else:
40 right = l[3:]
[8228]41 if '/' not in right:
42 addr = right
43 mask = 32
44 else:
45 addr, mask = right.split('/')
46 if addr in substs:
47 addr = substs[addr]
48 try:
49 addr = parseaddr(addr)
50 mask = int(mask)
51 addr = addr & ~((1 << (32 - mask)) - 1)
52 for i in range(0, (1 << (32 - mask))):
53 bezet[addr + i] = 1
54 except ValueError, e:
55 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l)
[3130]56
[8228]57 # Fetching smallest in use IP
58 # XXX: Currently not used for anything
59 i = parseaddr("255.255.255.255")
60 for k in bezet.keys():
61 if k < i:
62 i = k
63
64 # Interlinks are living in special ranges usually
65 if interlink:
66 i = parseaddr("172.16.3.0")
67 else:
68 i = parseaddr("172.17.0.0")
69
70 ranges = []
71 for n in xrange(aantal):
72 okay = False
73 while not okay:
74 while bezet.has_key(i):
75 i = i + numaddrs
76
77 # Assume Ok, till we find a already used IP
78 okay = True
79 for j in range(numaddrs):
80 if bezet.has_key(i + j):
81 i = i + numaddrs
82 okay = False
83 break
84 ranges.append(i)
85 bezet[i] = 1
[3130]86
[8228]87 return ranges
[3130]88
[8228]89def main():
90 if len(sys.argv) < 2:
91 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
92 print "Voorbeelden:"
93 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
94 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
95 exit(1)
96
97 # Argument parsing
98 interlink = sys.argv[1] == 'interlink'
99 size = int(sys.argv[2])
100 try:
101 aantal = int(sys.argv[3])
102 except (KeyError,IndexError):
103 aantal = 1
104
105 ranges = get_ranges(interlink, size, aantal)
106 numaddrs = 1 << (32 - size)
[8227]107
[8228]108 for i in ranges:
109 print "%s/%d:" % (showaddr(i), size),
110 if size > 28:
111 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
112 else:
113 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
114 print "...",
115 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
[3130]116
[8228]117if __name__ == "__main__":
118 main()
Note: See TracBrowser for help on using the repository browser.