[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] | 11 | import re
|
---|
[4150] | 12 | import sys
|
---|
[7936] | 13 | import glob
|
---|
[10051] | 14 | import gformat
|
---|
[3130] | 15 |
|
---|
| 16 | def 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] | 24 | def 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] | 28 | bezet_cache = None
|
---|
[13321] | 29 | def 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 | 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 | bezet_cache = bezet
|
---|
| 63 | return bezet
|
---|
| 64 |
|
---|
| 65 |
|
---|
[11514] | 66 | def get_ranges(interlink, size, aantal, use_history=False):
|
---|
| 67 | global bezet_cache
|
---|
[10051] | 68 | numaddrs = 1 << (32 - size)
|
---|
| 69 |
|
---|
[11514] | 70 | if not use_history or (use_history and not bezet_cache):
|
---|
[13321] | 71 | bezet = make_cache()
|
---|
[11514] | 72 | else:
|
---|
| 73 | bezet = bezet_cache
|
---|
[10051] | 74 |
|
---|
[11514] | 75 |
|
---|
[10051] | 76 | # Interlinks are living in special ranges usually
|
---|
| 77 | if interlink:
|
---|
| 78 | i = parseaddr("172.16.3.0")
|
---|
| 79 | else:
|
---|
| 80 | i = parseaddr("172.17.0.0")
|
---|
| 81 |
|
---|
| 82 | ranges = []
|
---|
| 83 | for n in xrange(aantal):
|
---|
| 84 | okay = False
|
---|
| 85 | while not okay:
|
---|
| 86 | while bezet.has_key(i):
|
---|
| 87 | i = i + numaddrs
|
---|
| 88 |
|
---|
| 89 | # Assume Ok, till we find a already used IP
|
---|
| 90 | okay = True
|
---|
| 91 | for j in range(numaddrs):
|
---|
| 92 | if bezet.has_key(i + j):
|
---|
| 93 | i = i + numaddrs
|
---|
| 94 | okay = False
|
---|
| 95 | break
|
---|
| 96 | ranges.append(i)
|
---|
| 97 | bezet[i] = 1
|
---|
| 98 |
|
---|
[11514] | 99 | bezet_cache = bezet
|
---|
[10051] | 100 | return ranges
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 |
|
---|
[8228] | 104 | def main():
|
---|
[10051] | 105 | if len(sys.argv) < 2:
|
---|
| 106 | print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
|
---|
| 107 | print "Voorbeelden:"
|
---|
| 108 | print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
|
---|
| 109 | print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
|
---|
| 110 | exit(1)
|
---|
| 111 |
|
---|
| 112 | # Argument parsing
|
---|
| 113 | interlink = sys.argv[1] == 'interlink'
|
---|
| 114 | size = int(sys.argv[2])
|
---|
| 115 | try:
|
---|
| 116 | aantal = int(sys.argv[3])
|
---|
| 117 | except (KeyError,IndexError):
|
---|
| 118 | aantal = 1
|
---|
| 119 |
|
---|
| 120 | ranges = get_ranges(interlink, size, aantal)
|
---|
| 121 | numaddrs = 1 << (32 - size)
|
---|
[8227] | 122 |
|
---|
[10051] | 123 | for i in ranges:
|
---|
| 124 | print "%s/%d:" % (showaddr(i), size),
|
---|
| 125 | if size > 28:
|
---|
| 126 | print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
|
---|
| 127 | else:
|
---|
| 128 | print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
|
---|
| 129 | print "...",
|
---|
| 130 | print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
|
---|
[3130] | 131 |
|
---|
[8228] | 132 | if __name__ == "__main__":
|
---|
| 133 | main()
|
---|