source: genesis/nodes/getrange.py@ 8064

Last change on this file since 8064 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
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
[4150]9size = 30
[7325]10if len(sys.argv) < 2:
11 print "Gebruik: python getrange.py <interlink|subnet> <size>"
[7972]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'"
[7971]14 exit(1)
15
[7325]16interlink = sys.argv[1] == 'interlink'
17size = int(sys.argv[2])
[4150]18numaddrs = 1 << (32 - size)
19
[7936]20substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';")
[3130]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
[7936]34for fname in glob.glob("*/wleiden.conf"):
35 substs = {}
[7972]36 lineNr = 0
[7936]37 for l in open(fname):
[7972]38 lineNr += 1
[7936]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]
[7972]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)
[3130]59
60i = 0xffffffffL
61for k in bezet.keys():
62 if k < i:
63 i = k
64
[6024]65
66i = 0xac100300
[7325]67if not interlink:
[5519]68 i = 0xac110000
[3130]69while bezet.has_key(i):
[4150]70 i = i + numaddrs
[3130]71
[4150]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.