Changeset 10051 in genesis
- Timestamp:
- Mar 7, 2012, 6:27:16 PM (13 years ago)
- Location:
- tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/getrange.py
r9516 r10051 1 1 #!/usr/bin/env python 2 # zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink 3 # range 4 # lodewijk@wirelessleiden.nl 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 # 5 11 import re 6 12 import sys 7 13 import glob 14 import gformat 8 15 9 16 def parseaddr(s): 10 f = s.split('.') 11 return (long(f[0]) << 24L) + \ 12 (long(f[1]) << 16L) + \ 13 (long(f[2]) << 8L) + \ 14 long(f[3]) 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 15 23 16 24 def showaddr(a): 17 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff) 25 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff) 26 18 27 19 28 def get_ranges(interlink, size, aantal): 20 numaddrs = 1 << (32 - size) 21 22 substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';") 23 24 25 bezet = {} 26 27 for fname in glob.glob("*/wleiden.conf"): 28 substs = {} 29 lineNr = 0 30 for l in open(fname): 31 lineNr += 1 32 l = l[:-1] 33 match = substre.match(l) 34 if match != None: 35 substs[match.group(1)] = match.group(2) 36 if l.startswith("IP=") or l.startswith("$master_ip="): 37 if l.startswith("$master_ip="): 38 right = l.split("'")[1] 39 else: 40 right = l[3:] 41 if '/' not in right: 42 addr = right 43 mask = 32 44 else: 45 addr, mask = right.split('/') 46 if addr in substs: 47 addr = substs[addr] 48 try: 49 addr = parseaddr(addr) 50 mask = int(mask) 51 addr = addr & ~((1 << (32 - mask)) - 1) 52 for i in range(0, (1 << (32 - mask))): 53 bezet[addr + i] = 1 54 except ValueError, e: 55 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l) 29 numaddrs = 1 << (32 - size) 30 31 bezet = {} 32 substs = {} 33 def add_ip(address): 34 if '/' not in address: 35 address += "/32" 56 36 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 64 # Interlinks are living in special ranges usually 65 if interlink: 66 i = parseaddr("172.16.3.0") 67 else: 68 i = parseaddr("172.17.0.0") 69 70 ranges = [] 71 for n in xrange(aantal): 72 okay = False 73 while not okay: 74 while bezet.has_key(i): 75 i = i + numaddrs 76 77 # Assume Ok, till we find a already used IP 78 okay = True 79 for j in range(numaddrs): 80 if bezet.has_key(i + j): 81 i = i + numaddrs 82 okay = False 83 break 84 ranges.append(i) 85 bezet[i] = 1 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) 86 51 87 return ranges 52 add_ip(datadump['masterip']) 53 for key in datadump['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 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 88 89 89 90 def main(): 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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) 107 108 108 109 110 111 112 113 114 115 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)]) 116 117 117 118 if __name__ == "__main__": -
tools/gformat.py
r10049 r10051 365 365 f = open(gfile, 'r') 366 366 datadump = yaml.load(f,Loader=Loader) 367 datadump['iface_keys'] = get_interface_keys(datadump) 367 368 f.close() 368 369 … … 374 375 375 376 f = open(gfile, 'w') 377 del datadump['iface_keys'] 376 378 f.write(generate_wleiden_yaml(datadump)) 377 379 f.close() … … 504 506 output += generate_wleiden_yaml(datadump) 505 507 elif config == 'authorized_keys': 506 f = open( "global_keys", 'r')508 f = open(os.path.join(NODE_DIR,"global_keys"), 'r') 507 509 output += f.read() 508 510 f.close()
Note:
See TracChangeset
for help on using the changeset viewer.