3

I want to automate a network topology diagram using python. I'm new to python so please bear with me. After doing some research I found out that I can use python to create graphml files which can be read by yEd. I'm learning how to use Networkx to create the graphml files. So far I'm able to create nodes, connect them and add labels to the nodes (these labels would be the hostnames). Now I need to know how I can add labels to the edges (these labels would be the interfaces). For example:

Topology example

If possible I would like to know how to add a custom image for every node (by default the shape is a square but I would like to use a router png file). If it is not possible then it would be helpful to know how to edit the height and width of the shape and also disabling arrows. I've reviewed the docs on networkx website but I haven't found how to do these changes directly to the graph object. The only way I've seen it done is when drawing the graph, for example using the following function: nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=False), but this is not what I need because this is not saved to the graphml file. If someone can guide me through this it would be really helpful, I'm attaching my code:

import networkx as nx
import matplotlib
import matplotlib.pyplot as plt

g = nx.DiGraph()

g.add_node('Hostname_A')
g.add_node('Hostname_B')
g.add_node('Hostname_C')
g.add_node('Hostname_D')

g.add_edge('Hostname_A','Hostname_B')
g.add_edge('Hostname_A','Hostname_C')
g.add_edge('Hostname_B','Hostname_D')
g.add_edge('Hostname_B','Hostname_C')

for node in g.nodes():
    g.node[node]['label'] = node

nx.readwrite.write_graphml(g, "graph.graphml")
john doe
  • 81
  • 4
  • Maybe related to https://stackoverflow.com/questions/34617307/how-do-i-customize-the-display-of-edge-labels-in-networkx ? You should check it out and explain if what you are looking for is different from this. – Eskapp Jun 26 '18 at 18:31
  • @Eskapp I checked that link before posting. It is different because I need the changes done to the labels of the edges to be saved to the graphml file. – john doe Jun 26 '18 at 19:05
  • Could you use something like `g.add_edge('Hostname_A', 'Hostname_B', label='your_label')` ? ('label' would then be a property of the edge) I think this is valid from networkx 2.+ only. – Eskapp Jun 27 '18 at 18:18
  • @Eskapp Hi there, I solved it yesterday using something like: `for edge in g.edges(): g.edges[edge]['source'] = 'int gi0/0/0' g.edges[edge]['destination'] = 'int gi0/0/1'` As you said these labels are property of the edges and can be mapped in yED. – john doe Jun 27 '18 at 19:10

1 Answers1

0

This is the solution:

for edge in g.edges(): 
g.edges[edge]['source'] = 'int gi0/0/0' 
g.edges[edge]['destination'] = 'int gi0/0/1'
john doe
  • 81
  • 4