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

Viewed 27

Goal is to send multiple 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 etc...

Where I am confuse, 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 don't want to create 200+ ssh connections if I use a function for the whole 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"}

print(mng_ip)

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:
        net_connect.enable()
        result = net_connect.send_command(f"ping {ip} ",delay_factor=2) # <-----  this is the part i would like to perform the threading 
        print(result)
        if "0/5" in result:
            print("FAILED")
        else:
            print("ACTIVE")
            active_list.append(ip)

net_connect.disconnect()

print("Done")
pprint(active_list)

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,

0 Answers
Related