DEV Community 👩‍💻👨‍💻

Cover image for Automating DrawIO network topology using Python
Amr ElHusseiny
Amr ElHusseiny

Posted on

Automating DrawIO network topology using Python


In this blog , we are going to show how to use the draw_network_plot python library to generate a DrawIO Network topology easily instead of manually drawing them yourself .

DrawIO is widely used as a free alternative to Microsoft's Visio to draw network topologies.

I will be using Netmiko and CDP to acquire the data needed for the plot for the devices themselves , but this part won't be the focus of the article .

Focus of the article is how to use the "drawio_network_plot" library" to generate the DrawIO file .

Library setup

Make sure to have Python 3.7 and above , then install the library using :
$ pip install drawio_network_plot

Lab setup

Lab is done on EVE-NG using Cisco's 7200VXR image to create the topology , the whole topology is L3 with Loopback so the Python CentOS server would be able to ssh to each device .

EVE-NG Topology Diagram (Non-generated):

Image description

Configuration and Script

You can find both the below script and all the EVE-NG Cisco devices configuration in the following Github Link :

from netmiko import ConnectHandler
import re 
from drawio_network_plot.drawio_network_plot import NetPlot

# ----------------- Getting live CDP Neigbors to gather links endpoints ---------------------------
def retieve_lldp_neigbor_hostname(devices_list):
    list_of_cdp_neighborship = []
    for device in devices_list:
        device_dictionary = {
                            'device_type': 'cisco_ios',  
                            'host': device['ip_address'],
                            'username': 'automation',
                            'password' : '1234567',
        net_connect = ConnectHandler(**device_dictionary)
        output = net_connect.send_command("show cdp neighbor")
        lines_list = output.splitlines()
        for line in lines_list:
                # regex to search the name of the device before the domain ID ".default" , then removing the word ".default" from the string
                link = {
                            'sourceNodeID' : device['nodeName'],
                            'destinationNodeID' :'\S+\.default',line).group().replace('.default','') 
                # checking for duplication before adding new link : 
                if {'sourceNodeID':link['destinationNodeID'],'destinationNodeID':link['sourceNodeID']} not in list_of_cdp_neighborship:
    return list_of_cdp_neighborship

def main():
    # Lab Devices , must have the device type for the plotting library to work 
    devices = [
    # Getting list of links for each device
    list_of_cdp_neighborship = retieve_lldp_neigbor_hostname(devices)
    for peering in list_of_cdp_neighborship:

    # ------------------------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------
    # ----------------- Main Part : using library to generate XML DrawIIO format ---------------------------
    # Using the Plot library 
    x = NetPlot()
    # ------------------------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------

if __name__ == "__main__":
Enter fullscreen mode Exit fullscreen mode

Library Options

Remember , you can gather the data however you like , this was just a simple demonstration of what you can do , main code to remember is the plotting part :

# please adhere to the naming scheme in the variables 
device_list = [{'nodeName' : 'TOR_1','nodeType' : 'l2_switch','nodeDescription' : 'Leaf Switch 01'}]
x = NetPlot()
# **IMPORTANT NOTE :**---> Make sure that the sourceNode in the connection is the higher level device and that connections are not replicated , this way when you use the DrawIO automatic layout , it would create the diagram hierarchy the correct way 
connections_list = [{'sourceNodeID' : 'Router_1','destinationNodeID' : 'Core_switch_1'}]
    # Adding using list all at once
    # Adding node by node and link by link

    # --- Output ---
    # You can print the XML to the Stdout 
    # Or You can also directly generate an XML file using the built in function :
    # x.exportXML('examples/output.xml')    
Enter fullscreen mode Exit fullscreen mode

Generated output will be collapsed in one point , you will have to choose the Layout you would like in DrawIO after opening the file like showed as follows :

Generated Topology

1- Open generated XML file in DrawIO
Initial Generated File

2- go to Arrange/Layout/Vertical Tree
go to Arrange/Layout/Vertical Tree

3- Final Result
Image description

For any comment on the pip package , feature addition or any comment , please share your suggestions as this is my 1st PIP library , and divinely its not perfect .

Thanks ...

Top comments (1)

arunprakash142 profile image

I like this article very much. The content was good. If any of the engineering students are looking for deep learning final year projects, I found this site and they are providing the best service to the engineering students regarding the projects..

🌱 DEV runs on 100% open source code that we started called Forem.

You can contribute to the codebase or host your own.