Netmiko / Python Threading for multiple send_command on the same devices ( network device )


This Content is from Stack Overflow. Question asked by Patrick Denis

Goal send an ping via a Cisco device ( L2 ) to populate the arp table.

Script is done and working but ultra slow since I need to ping 254 address and more depending if /24 or /23.

Where I am confuse, is that I have test some threading with some basic scripts to understand how it works and everything works by using a function and call it and so far so good.

My Problem is that I dont want to create 200+ ssh connections if i use a function for my code
. I would like to use the threading only on the send_command part from netmiko.

My Code :

from netmiko import ConnectHandler
from pprint import pprint
from time import perf_counter
from threading import Thread

mng_ip = input("please enter the subnet to be scan: ")

cisco_Router = {
    "device_type": "cisco_ios",
    "host": mng_ip,
    "username": "XXXX",
    "password": "XXXX",
    "fast_cli": False,
    "secret": "XXXX"}


subnet1 = mng_ip.split(".")[0]
subnet2 = mng_ip.split(".")[1]
subnet3 = mng_ip.split(".")[2]
#subnet4 = mng_ip.split(".")[3]

active_list = []

start_time = perf_counter()

for x in range(1,254):
    ip = (subnet1+"."+subnet2+"."+subnet3+"."+str(x))
    print ("Pinging:",ip)

    with ConnectHandler(**cisco_Router) as net_connect:
        result = net_connect.send_command(f"ping {ip} ",delay_factor=2) # <-----  this is the part i would like to perform the threading 
        if "0/5" in result:



end_time = perf_counter()

print(f'It took {end_time- start_time: 0.2f} second(s) to complete.')

not sure if it is possible and how it could be done,

Thank you in advance,


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?