Changeset 8228 in genesis
- Timestamp:
- Aug 4, 2010, 7:02:34 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
nodes/getrange.py
r8227 r8228 6 6 import sys 7 7 import glob 8 9 size = 3010 if len(sys.argv) < 2:11 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]12 print "Voorbeelden:"13 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]14 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]15 exit(1)16 17 interlink = sys.argv[1] == 'interlink'18 size = int(sys.argv[2])19 try:20 aantal = int(sys.argv[3])21 except (KeyError,IndexError):22 aantal = 123 numaddrs = 1 << (32 - size)24 25 substre = re.compile("^(\$[a-zA-Z][^ =]*) ?= ?'([^']*)';")26 8 27 9 def parseaddr(s): … … 35 17 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff) 36 18 37 bezet = {} 19 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="): 37 right = l[3:] 38 if '/' not in right: 39 addr = right 40 mask = 32 41 else: 42 addr, mask = right.split('/') 43 if addr in substs: 44 addr = substs[addr] 45 try: 46 addr = parseaddr(addr) 47 mask = int(mask) 48 addr = addr & ~((1 << (32 - mask)) - 1) 49 for i in range(0, (1 << (32 - mask))): 50 bezet[addr + i] = 1 51 except ValueError, e: 52 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l) 38 53 39 for fname in glob.glob("*/wleiden.conf"): 40 substs = {} 41 lineNr = 0 42 for l in open(fname): 43 lineNr += 1 44 l = l[:-1] 45 match = substre.match(l) 46 if match != None: 47 substs[match.group(1)] = match.group(2) 48 if l.startswith("IP="): 49 right = l[3:] 50 if '/' not in right: 51 addr = right 52 mask = 32 53 else: 54 addr, mask = right.split('/') 55 if addr in substs: 56 addr = substs[addr] 57 try: 58 addr = parseaddr(addr) 59 mask = int(mask) 60 addr = addr & ~((1 << (32 - mask)) - 1) 61 for i in range(0, (1 << (32 - mask))): 62 bezet[addr + i] = 1 63 except ValueError, e: 64 print "[FOUT] in bestand '%s' regel '%i': %s" % (fname, lineNr, l) 54 # Fetching smallest in use IP 55 # XXX: Currently not used for anything 56 i = parseaddr("255.255.255.255") 57 for k in bezet.keys(): 58 if k < i: 59 i = k 60 61 # Interlinks are living in special ranges usually 62 if interlink: 63 i = parseaddr("172.16.3.0") 64 else: 65 i = parseaddr("172.17.0.0") 66 67 ranges = [] 68 for n in xrange(aantal): 69 okay = False 70 while not okay: 71 while bezet.has_key(i): 72 i = i + numaddrs 73 74 # Assume Ok, till we find a already used IP 75 okay = True 76 for j in range(numaddrs): 77 if bezet.has_key(i + j): 78 i = i + numaddrs 79 okay = False 80 break 81 ranges.append(i) 82 bezet[i] = 1 65 83 66 # Fetching smallest in use IP 67 # XXX: Currently not used for anything 68 i = parseaddr("255.255.255.255") 69 for k in bezet.keys(): 70 if k < i: 71 i = k 84 return ranges 72 85 73 # Interlinks are living in special ranges usually 74 if interlink: 75 i = parseaddr("172.16.3.0") 76 else: 77 i = parseaddr("172.17.0.0") 86 def main(): 87 if len(sys.argv) < 2: 88 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0] 89 print "Voorbeelden:" 90 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0] 91 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0] 92 exit(1) 93 94 # Argument parsing 95 interlink = sys.argv[1] == 'interlink' 96 size = int(sys.argv[2]) 97 try: 98 aantal = int(sys.argv[3]) 99 except (KeyError,IndexError): 100 aantal = 1 101 102 ranges = get_ranges(interlink, size, aantal) 103 numaddrs = 1 << (32 - size) 78 104 79 for n in xrange(aantal): 80 okay = False 81 while not okay: 82 while bezet.has_key(i): 83 i = i + numaddrs 84 85 # Assume Ok, till we find a already used IP 86 okay = True 87 for j in range(numaddrs): 88 if bezet.has_key(i + j): 89 i = i + numaddrs 90 okay = False 91 break 105 for i in ranges: 106 print "%s/%d:" % (showaddr(i), size), 107 if size > 28: 108 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)]) 109 else: 110 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]), 111 print "...", 112 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)]) 92 113 93 print "%s/%d:" % (showaddr(i), size), 94 if size > 28: 95 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)]) 96 else: 97 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]), 98 print "...", 99 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)]) 100 bezet[i] = 1 114 if __name__ == "__main__": 115 main()
Note:
See TracChangeset
for help on using the changeset viewer.