source: genesis/tools/getrange.py@ 14378

Last change on this file since 14378 was 14378, checked in by rick, 5 years ago

Fix getrange.py not working due python2

FreeBSD tends to default to python3 these days, so time to start converting all
stuff to python3.

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