source: genesis/tools/getrange.py@ 10295

Last change on this file since 10295 was 10052, checked in by rick, 13 years ago

Whoops clashing keys namespace.

Related-To ticket:117

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