【问题标题】:Which method is used for Multithreading when using the NAPALM library with Python?在 Python 中使用 NAPALM 库时,哪种方法用于多线程?
【发布时间】:2022-08-21 17:11:21
【问题描述】:

我下面的代码是在设备上输入配置或检查设备是否有配置。

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()

我能够为单个 IP 制作此代码。您建议我将哪种线程方法用于 NAPALM 的多个进程?

    标签: python multithreading networking napalm


    【解决方案1】:

    我对您的代码进行了一些编辑。您可以运行以下代码。

    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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多