Skip to content

Instantly share code, notes, and snippets.

@natenka
Last active October 17, 2017 02:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natenka/4d991cacc69e7353c84504c1581a5014 to your computer and use it in GitHub Desktop.
Save natenka/4d991cacc69e7353c84504c1581a5014 to your computer and use it in GitHub Desktop.
# Based on http://matthiaseisen.com/articles/graphviz/
import graphviz as gv
styles = {
'graph': {
'label': 'Network Map',
'fontsize': '16',
'fontcolor': 'white',
'bgcolor': '#333333',
'rankdir': 'BT',
},
'nodes': {
'fontname': 'Helvetica',
'shape': 'box',
'fontcolor': 'white',
'color': '#006699',
'style': 'filled',
'fillcolor': '#006699',
'margin': '0.4',
},
'edges': {
'style': 'dashed',
'color': 'green',
'arrowhead': 'open',
'fontname': 'Courier',
'fontsize': '14',
'fontcolor': 'white',
}
}
def apply_styles(graph, styles):
graph.graph_attr.update(
('graph' in styles and styles['graph']) or {}
)
graph.node_attr.update(
('nodes' in styles and styles['nodes']) or {}
)
graph.edge_attr.update(
('edges' in styles and styles['edges']) or {}
)
return graph
def draw_topology(topology_dict, output_filename='img/topology'):
nodes = set([key[0] for key in topology_dict.keys() + topology_dict.values()])
g1 = gv.Graph(format='svg')
for node in nodes:
g1.node(node)
for key, value in topology_dict.iteritems():
head, t_label = key
tail, h_label = value
g1.edge(head, tail, headlabel=h_label, taillabel=t_label, label=" "*12)
# headport='nw' tailport='s'
g1 = apply_styles(g1, styles)
print(g1.source)
filename = g1.render(filename=output_filename)
print "Graph saved in", filename
diag5 = {('R3', 'Fa0/1'): ('R1', 'Fa0/1'),
('R2', 'Fa3/0'): ('R1', 'Fa3/0'),
('R2', 'Fa1/0'): ('R1', 'Fa0/1'),
('R4', 'Fa4/0'): ('R1', 'Fa4/0'),
('R2', 'Fa0/0'): ('R3', 'Fa0/0'),
('R5', 'Fa4/0'): ('R3', 'Fa3/0'),
('R4', 'Fa1/0'): ('R3', 'Fa0/0'),
('R4', 'Fa1/0'): ('R2', 'Fa1/0'),
('R4', 'Fa0/1'): ('R5', 'Fa0/1')}
draw_topology(diag5)
@natenka
Copy link
Author

natenka commented Feb 2, 2017

img/topology.svg:

topology

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment