source: genesis/tools/getrange.py@ 12356

Last change on this file since 12356 was 11514, checked in by rick, 12 years ago

Nasty hack for calling function multiple times without the need of:

1) parsing all config files all the time.
2) Remembering the previous spit out entries.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.1 KB
RevLine 
[7089]1#!/usr/bin/env python
[10051]2#
3# Zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink
4# range.
5#
6# Maybe this should use the py-iplib module?
7#
8# Orignal: lodewijk@wirelessleiden.nl
9# 07/March/2012 - Rick van der Zwet <info@rickvanderzwet.nl>
10#
[3130]11import re
[4150]12import sys
[7936]13import glob
[10051]14import gformat
[3130]15
16def parseaddr(s):
[10051]17 f = s.split('.')
18 return (long(f[0]) << 24L) + \
19 (long(f[1]) << 16L) + \
20 (long(f[2]) << 8L) + \
21 long(f[3])
[3130]22
[10051]23
[3130]24def showaddr(a):
[10051]25 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
[3130]26
[10051]27
[11514]28bezet_cache = None
29def get_ranges(interlink, size, aantal, use_history=False):
30 global bezet_cache
[10051]31 numaddrs = 1 << (32 - size)
32
[11514]33 if not use_history or (use_history and not bezet_cache):
34 bezet = {}
35 substs = {}
36 def add_ip(address):
37 if '/' not in address:
38 address += "/32"
[3130]39
[11514]40 addr, mask = address.split('/')
41 if addr in substs:
42 addr = substs[addr]
43 try:
44 addr = parseaddr(addr)
45 mask = int(mask)
46 addr = addr & ~((1 << (32 - mask)) - 1)
47 for i in range(0, (1 << (32 - mask))):
48 bezet[addr + i] = 1
49 except ValueError, e:
50 print "[FOUT] in address %s node %s" % (address, node)
51
52 for node in gformat.get_hostlist():
53 datadump = gformat.get_yaml(node)
[3130]54
[11514]55 add_ip(datadump['masterip'])
56 for key in datadump['autogen_iface_keys']:
57 add_ip(datadump[key]['ip'])
[3130]58
[11514]59 # Fetching smallest in use IP
60 # XXX: Currently not used for anything
61 i = parseaddr("255.255.255.255")
62 for k in bezet.keys():
63 if k < i:
64 i = k
65 else:
66 bezet = bezet_cache
[10051]67
[11514]68
[10051]69 # Interlinks are living in special ranges usually
70 if interlink:
71 i = parseaddr("172.16.3.0")
72 else:
73 i = parseaddr("172.17.0.0")
74
75 ranges = []
76 for n in xrange(aantal):
77 okay = False
78 while not okay:
79 while bezet.has_key(i):
80 i = i + numaddrs
81
82 # Assume Ok, till we find a already used IP
83 okay = True
84 for j in range(numaddrs):
85 if bezet.has_key(i + j):
86 i = i + numaddrs
87 okay = False
88 break
89 ranges.append(i)
90 bezet[i] = 1
91
[11514]92 bezet_cache = bezet
[10051]93 return ranges
94
95
96
[8228]97def main():
[10051]98 if len(sys.argv) < 2:
99 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
100 print "Voorbeelden:"
101 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
102 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
103 exit(1)
104
105 # Argument parsing
106 interlink = sys.argv[1] == 'interlink'
107 size = int(sys.argv[2])
108 try:
109 aantal = int(sys.argv[3])
110 except (KeyError,IndexError):
111 aantal = 1
112
113 ranges = get_ranges(interlink, size, aantal)
114 numaddrs = 1 << (32 - size)
[8227]115
[10051]116 for i in ranges:
117 print "%s/%d:" % (showaddr(i), size),
118 if size > 28:
119 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
120 else:
121 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
122 print "...",
123 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
[3130]124
[8228]125if __name__ == "__main__":
126 main()
Note: See TracBrowser for help on using the repository browser.