Which method is used for Multithreading when using the NAPALM library with Python?

170 views Asked by At

My code below is to enter a configuration on the device or check if the device has a configuration.

driver = get_network_driver("ios")

device = driver(hostname= X.X.X.X,
                    username='username',
                    password='password',
                    optional_args={'port': 22})

device.open()

print("Accesing device")
device.load_merge_candidate(filename="syslog.txt")

diffs = device.compare_config()

if len(diffs) > 0:
    print(diffs)
    device.commit_config()
else:
    print("no changes required")
    device.discard_config()

device.close()

I was able to make this code for a single IP. Which Threading method would you recommend I use for multiple processes with NAPALM?

1

There are 1 answers

0
Baris Sonmez On BEST ANSWER

I made a few edits to your code. You can run the below code.

from napalm import get_network_driver
from netmiko import NetMikoAuthenticationException
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from getpass import getpass
import json
from termcolor import colored
import time
start = time.time()
username = input('username: ')
password = getpass('password:')
driver = get_network_driver('ios')

with open('devices.txt', 'r') as router_ips:
        for ip in router_ips:
                print('connecting to', ip)
                optional_args = {'secret': 'cisco'}
                try:
                        device = driver(ip, username, password,  optional_args={'global_delay_factor': 4})
                        device.open()
                except NetMikoAuthenticationException:
                        print(colored('Authentication Error!!!', 'red'))
                        username = input('username: ')
                        password = getpass('password:')
                        device = driver(ip, username, password, optional_args={'global_delay_factor': 4})
                except NetMikoTimeoutException:
                        username = input('username: ')
                        password = getpass('password:')
                        device = driver(ip, username, password, optional_args={'global_delay_factor': 4})
                        print(colored('Time exceed!!!', 'red'))
                except SSHException:
                        print(colored('Transport error with ssh or authentication', 'red'))
                else:
                        device.load_merge_candidate('syslog.txt')
                        diff = device.compare_config()
                        print('Below configuration will be push on device')
                        print((diff))
                        while True:
                                if len(diff) > 0:
                                        userinp = input('Do you want to commit changes on device, hit Y or  N: ').lower().strip()
                                        if userinp == 'y':
                                                device.commit_config()
                                                print(colored('\nCommitting changes on devise', 'yellow'))
                                                print('#' * 50)
                                                print(colored('\nCommit confirm...', 'green'))
                                                break
                                        elif userinp == 'n':
                                                print(colored('\nDiscard the changes', 'red'))
                                                device.discard_config()
                                                break
                                        else:
                                                print(colored('\ninvalid input detected', 'blue'))
                        device.close()
end = time.time()
print(f'Total time during execution is:', colored(end - start, 'red'))