source: fedd/deter/topdl_to_route.py @ 8fbef04

Last change on this file since 8fbef04 was e8f2d4c, checked in by Ted Faber <faber@…>, 11 years ago

Add this

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#!/usr/local/bin/python
2
3import topdl
4import StringIO
5
6def topology_to_route_file(top, filename=None, file=None):
7    # Outfile will point to the output target, either a file or a string buffer
8    outf = None
9    closeit = True
10    if all([filename, file]): return False
11    elif file: 
12        closeit = False
13        outf = file
14    elif filename: outf = open(filename, 'w')
15    else: outf = StringIO.StringIO()
16
17    for e in top.elements:
18        if not isinstance(e, topdl.Computer): continue
19        myips = set()
20        mylinks = { }
21        for inf in e.interface:
22            ip = inf.get_attribute('ip4_address')
23            if ip: myips.add(ip)
24            for sub in inf.subs:
25                for oinf in sub.interfaces:
26                    dip = oinf.get_attribute('ip4_address')
27                    if dip is not None and dip not in myips:
28                        if ip not in mylinks: mylinks[ip] = []
29                        mylinks[ip].append(dip)
30        print >>outf, '%s: %s' % (e.name, ','.join(myips))
31        for ip, links in mylinks.items():
32            print >>outf, '%s: %s' % (ip, ','.join(links))
33    outf.flush()
34    # If this routine built a string, return it
35    if isinstance(outf, StringIO.StringIO): rv = outf.getvalue()
36    else: rv = True
37    if closeit: outf.close()
38    return rv
Note: See TracBrowser for help on using the repository browser.