Scrapli & Flask Project Multi Threading Device Commands

Although this is similar to the previous post of Scrapli Async and Multi threading. The difference is here is that it is a working example of what will be used in the Scrapli & Flask Project.

In the code below I will be using Scrapli to connect to 8 of my devices in the GNS3 lab to get the version. The number of devices is doubled to 16, reusing the first 8, so checking the version twice.

I have an update to this below that is how I have incorporated it into Flask.

Lab

Code Output

Code

The code below is the actual file from the Scrapli & Flask project, I have used this to test with a device list and what will likely happen in the working version of the Scrapli and Flask Project. Right now this is a working example of Scrapli using multithreading for a range of Cisco devices when only entering an IP address. There are still some things I need address with this script such as what happens if I have three device types. I will make an update once I have the Arista switches working to test with them.

This code took me a while to get working because of the try/except for the Scrapli driver version. The Nexus switches were connecting but then taking a long time to time out. I think 15 seconds is the default timeout for connection, prompt and to get a return once a command is sent.
I used the three timeouts above to modify the behaviour. Before the timeout changes to get the version of 8 devices it was taking 30+ seconds. Now for 16 devices it is down to 8 seconds.

More Scrapli timeout reading can be found here

Update! Adding to Flask

I have made numerous changes to integrate the code above into flask. The main two were; the creation of the threads into its own function and error checking.

Below is the form that the IPS are input. And below that is the output to the console when IPs are submited through the Flask form.
I have used a list of all 8 IP addresses in my lab and four IP addresses that Scrapli will fail to connect to.
Right now there is no DB update, so Flask just redirects to the Device Inventory once the Scapli script has run.

Leave a Comment

Your email address will not be published. Required fields are marked *