source: genesis/nodes/getrange.py@ 8227

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

Functional changes:

  • Allow number of ranges to be requested

Cosmetic changes:

  • Instead of the binary foo, use simple IP display addresses in code


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