source: genesis/tools/getrange.py@ 14380

Last change on this file since 14380 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
RevLine 
[14378]1#!/usr/bin/env python3
[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#
[14378]11import gformat
12import glob
[3130]13import re
[4150]14import sys
[14378]15import textwrap
[3130]16
17def parseaddr(s):
[10051]18 f = s.split('.')
[14378]19 return (int(f[0]) << 24) + \
20 (int(f[1]) << 16) + \
21 (int(f[2]) << 8) + \
22 (int(f[3]))
[3130]23
[10051]24
[3130]25def showaddr(a):
[10051]26 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
[3130]27
[10051]28
[11514]29bezet_cache = None
[13321]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
[14378]47 except ValueError as e:
48 print("[FOUT] in address %s node %s" % (address, node))
[13321]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']:
[13641]55 if 'ip' in datadump[key]:
56 add_ip(datadump[key]['ip'])
[13321]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
[11514]68def get_ranges(interlink, size, aantal, use_history=False):
69 global bezet_cache
[10051]70 numaddrs = 1 << (32 - size)
71
[11514]72 if not use_history or (use_history and not bezet_cache):
[13321]73 bezet = make_cache()
[11514]74 else:
75 bezet = bezet_cache
[10051]76
[11514]77
[10051]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 = []
[14378]85 for n in range(aantal):
[10051]86 okay = False
87 while not okay:
[14378]88 while i in bezet:
[10051]89 i = i + numaddrs
90
91 # Assume Ok, till we find a already used IP
92 okay = True
93 for j in range(numaddrs):
[14378]94 if (i + j) in bezet:
[10051]95 i = i + numaddrs
96 okay = False
97 break
98 ranges.append(i)
99 bezet[i] = 1
100
[11514]101 bezet_cache = bezet
[10051]102 return ranges
103
104
105
[8228]106def main():
[10051]107 if len(sys.argv) < 2:
[14378]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] }))
[10051]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)
[8227]126
[10051]127 for i in ranges:
[14378]128 print("%s/%d: " % (showaddr(i), size), end='')
[10051]129 if size > 28:
[14378]130 print(" en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)]))
[10051]131 else:
[14378]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)]))
[3130]135
[8228]136if __name__ == "__main__":
137 main()
Note: See TracBrowser for help on using the repository browser.