#!/usr/local/bin/python import topdl import StringIO def topology_to_route_file(top, filename=None, file=None): # Outfile will point to the output target, either a file or a string buffer outf = None closeit = True if all([filename, file]): return False elif file: closeit = False outf = file elif filename: outf = open(filename, 'w') else: outf = StringIO.StringIO() for e in top.elements: if not isinstance(e, topdl.Computer): continue myips = set() mylinks = { } for inf in e.interface: ip = inf.get_attribute('ip4_address') if ip: myips.add(ip) for sub in inf.subs: for oinf in sub.interfaces: dip = oinf.get_attribute('ip4_address') if dip is not None and dip not in myips: if ip not in mylinks: mylinks[ip] = [] mylinks[ip].append(dip) print >>outf, '%s: %s' % (e.name, ','.join(myips)) for ip, links in mylinks.items(): print >>outf, '%s: %s' % (ip, ','.join(links)) outf.flush() # If this routine built a string, return it if isinstance(outf, StringIO.StringIO): rv = outf.getvalue() else: rv = True if closeit: outf.close() return rv