source: genesis/tools/getrange.py@ 13680

Last change on this file since 13680 was 13641, checked in by rick, 8 years ago

Not all interfaces has IP addresses anymore.

Checking whether IP is required or not is part of validation script and not
range finder.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.2 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
28bezet_cache = None
29def make_cache():
30 global bezet_cache
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 if 'ip' in datadump[key]:
55 add_ip(datadump[key]['ip'])
56
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 bezet_cache = bezet
64 return bezet
65
66
67def get_ranges(interlink, size, aantal, use_history=False):
68 global bezet_cache
69 numaddrs = 1 << (32 - size)
70
71 if not use_history or (use_history and not bezet_cache):
72 bezet = make_cache()
73 else:
74 bezet = bezet_cache
75
76
77 # Interlinks are living in special ranges usually
78 if interlink:
79 i = parseaddr("172.16.3.0")
80 else:
81 i = parseaddr("172.17.0.0")
82
83 ranges = []
84 for n in xrange(aantal):
85 okay = False
86 while not okay:
87 while bezet.has_key(i):
88 i = i + numaddrs
89
90 # Assume Ok, till we find a already used IP
91 okay = True
92 for j in range(numaddrs):
93 if bezet.has_key(i + j):
94 i = i + numaddrs
95 okay = False
96 break
97 ranges.append(i)
98 bezet[i] = 1
99
100 bezet_cache = bezet
101 return ranges
102
103
104
105def main():
106 if len(sys.argv) < 2:
107 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
108 print "Voorbeelden:"
109 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
110 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
111 exit(1)
112
113 # Argument parsing
114 interlink = sys.argv[1] == 'interlink'
115 size = int(sys.argv[2])
116 try:
117 aantal = int(sys.argv[3])
118 except (KeyError,IndexError):
119 aantal = 1
120
121 ranges = get_ranges(interlink, size, aantal)
122 numaddrs = 1 << (32 - size)
123
124 for i in ranges:
125 print "%s/%d:" % (showaddr(i), size),
126 if size > 28:
127 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
128 else:
129 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
130 print "...",
131 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
132
133if __name__ == "__main__":
134 main()
Note: See TracBrowser for help on using the repository browser.