ops

netmiko使用案列16 多线程和多进程的使用

2018-09-05  本文已影响0人  小黑佬

方法一

processes_netmiko.py

#!/usr/bin/env python
'''
Use processes and Netmiko to connect to each of the devices. Execute
'show version' on each device. Record the amount of time required to do this.
'''
from __future__ import print_function, unicode_literals
from multiprocessing import Process

from datetime import datetime
from netmiko import ConnectHandler
from my_devices import device_list as devices


def show_version(a_device):
    '''Execute show version command using Netmiko.'''
    remote_conn = ConnectHandler(**a_device)
    print()
    print('#' * 80)
    print(remote_conn.send_command("show version"))
    print('#' * 80)
    print()


def main():
    '''
    Use processes and Netmiko to connect to each of the devices. Execute
    'show version' on each device. Record the amount of time required to do this.
    '''
    start_time = datetime.now()

    procs = []
    for a_device in devices:
        my_proc = Process(target=show_version, args=(a_device,))
        my_proc.start()
        procs.append(my_proc)

    for a_proc in procs:
        print(a_proc)
        a_proc.join()

    print("\nElapsed time: " + str(datetime.now() - start_time))


if __name__ == "__main__":
    main()

my_devices.py

from getpass import getpass

std_pwd = 'admin@123'
#std_pwd = getpass("Enter standard password: ")

pynet_rtr1 = {
    'device_type': 'cisco_ios',
    'ip': '10.0.1.7',
    'username': 'admin',
    'password': std_pwd,
}

pynet_rtr2 = {
    'device_type': 'cisco_ios',
    'ip': '10.0.1.6',
    'username': 'admin',
    'password': std_pwd,
}

pynet_sw1 = {
    'device_type': 'arista_eos',
    'ip': '10.10.247.72',
    'username': 'admin',
    'password': std_pwd,
}

pynet_sw2 = {
    'device_type': 'arista_eos',
    'ip': '10.10.247.73',
    'username': 'admin',
    'password': std_pwd,
}

pynet_sw3 = {
    'device_type': 'arista_eos',
    'ip': '10.10.247.74',
    'username': 'admin',
    'password': std_pwd,
}

pynet_sw4 = {
    'device_type': 'arista_eos',
    'ip': '10.10.247.75',
    'username': 'admin',
    'password': std_pwd,
}

juniper_srx = {
    'device_type': 'juniper_junos',
    'ip': '10.10.247.76',
    'username': 'admin',
    'password': std_pwd,
}

#device_list = [
#        pynet_rtr1,
#        pynet_rtr2,
#        pynet_sw1,
#        pynet_sw2,
#        pynet_sw3,
#        pynet_sw4,
#        juniper_srx,
#]


device_list = [
        pynet_rtr1,
        pynet_rtr2,
]


image.png

方法2

processes_netmiko_queue.py

执行结果和上面一样

#!/usr/bin/env python
'''
Use processes and Netmiko to connect to each of the devices. Execute
'show version' on each device. Use a queue to pass the output back to the parent process.
Record the amount of time required to do this.
'''
from __future__ import print_function, unicode_literals
from multiprocessing import Process, Queue

from datetime import datetime
from netmiko import ConnectHandler
from my_devices import device_list as devices


def show_version_queue(a_device, output_q):
    '''
    Use Netmiko to execute show version. Use a queue to pass the data back to
    the main process.
    '''
    output_dict = {}
    remote_conn = ConnectHandler(**a_device)
    hostname = remote_conn.base_prompt
    output = ('#' * 80) + "\n"
    output += remote_conn.send_command("show version") + "\n"
    output += ('#' * 80) + "\n"
    output_dict[hostname] = output
    output_q.put(output_dict)


def main():
    '''
    Use processes and Netmiko to connect to each of the devices. Execute
    'show version' on each device. Use a queue to pass the output back to the parent process.
    Record the amount of time required to do this.
    '''
    start_time = datetime.now()
    output_q = Queue(maxsize=20)

    procs = []
    for a_device in devices:
        my_proc = Process(target=show_version_queue, args=(a_device, output_q))
        my_proc.start()
        procs.append(my_proc)

    # Make sure all processes have finished
    for a_proc in procs:
        a_proc.join()

    while not output_q.empty():
        my_dict = output_q.get()
        for k, val in my_dict.items():
            print(k)
            print(val)

    print("\nElapsed time: " + str(datetime.now() - start_time))


if __name__ == "__main__":
    main()

方法3

threads_netmiko.py

#!/usr/bin/env python
import threading
from datetime import datetime
from netmiko import ConnectHandler
from my_devices import device_list as devices


def show_version(a_device):
    '''Execute show version command using Netmiko.'''
    remote_conn = ConnectHandler(**a_device)
    print()
    print('#' * 80)
    print(remote_conn.send_command_expect("show version"))
    print('#' * 80)
    print()


def main():
    '''
    Use threads and Netmiko to connect to each of the devices. Execute
    'show version' on each device. Record the amount of time required to do this.
    '''
    start_time = datetime.now()

    for a_device in devices:
        my_thread = threading.Thread(target=show_version, args=(a_device,))
        my_thread.start()

    main_thread = threading.currentThread()
    for some_thread in threading.enumerate():
        if some_thread != main_thread:
            print(some_thread)
            some_thread.join()

    print("\nElapsed time: " + str(datetime.now() - start_time))


if __name__ == "__main__":
    main()
上一篇下一篇

猜你喜欢

热点阅读