[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 |
|
---|
[8228] | 28 | def get_ranges(interlink, size, aantal):
|
---|
[10051] | 29 | numaddrs = 1 << (32 - size)
|
---|
| 30 |
|
---|
| 31 | bezet = {}
|
---|
| 32 | substs = {}
|
---|
| 33 | def add_ip(address):
|
---|
| 34 | if '/' not in address:
|
---|
| 35 | address += "/32"
|
---|
[3130] | 36 |
|
---|
[10051] | 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)
|
---|
[3130] | 51 |
|
---|
[10051] | 52 | add_ip(datadump['masterip'])
|
---|
[10052] | 53 | for key in datadump['autogen_iface_keys']:
|
---|
[10051] | 54 | add_ip(datadump[key]['ip'])
|
---|
[3130] | 55 |
|
---|
[10051] | 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 |
|
---|
[8228] | 90 | def main():
|
---|
[10051] | 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)
|
---|
[8227] | 108 |
|
---|
[10051] | 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)])
|
---|
[3130] | 117 |
|
---|
[8228] | 118 | if __name__ == "__main__":
|
---|
| 119 | main()
|
---|