Python多处理Netmiko

2024-10-01 00:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的是python和网络自动化的新手; 我正在尝试用Python和netmiko进行多处理,但没有成功;每个设备都会按顺序执行代码

以下是我的代码和结果:

=============================================

import datetime
from netmiko import ConnectHandler
import threading
from time import time
import multiprocessing
from multiprocessing import Process, Lock


starting_time = time()

def newthread():
    with open('routers.txt', 'r') as devices:
        for line in devices:
            deviceip = line.strip()
            host = {
                'device_type': 'cisco_ios',
                'ip': deviceip,
                'username': 'cisco',
                'password': 'cisco',
                'secret': 'cisco'
            }

            try:
                connection = ConnectHandler(**host)
                print('Trying router', deviceip)
                print('Connection Established to Host:', deviceip)
                connection.enable()
                sendcommand = connection.send_command('sh run | i hostname')
                print(sendcommand)
            except:
                print('Connection Failed to host', deviceip)


threadtask = Process(target=newthread)
threadtask.start()
threadtask.join()

print('Time Elaspsed:', time() - starting_time)


====Result===
Trying router 10.10.32.2
Connection Established to Host: 10.10.32.2
hostname R1
Trying router 10.10.32.3
Connection Established to Host: 10.10.32.3
hostname R2
Trying router 10.10.32.4
Connection Established to Host: 10.10.32.4
hostname R4
Trying router 10.10.32.5
Connection Established to Host: 10.10.32.5
hostname R3
Time Elaspsed: 26.788068771362305

Process finished with exit code 0

我可能做错了什么?我有点卡住了。 多谢各位

__ 当做 戴斯蒙K


Tags: tofromimporthosttimeconnectionprocesscisco
3条回答

使用concurrent.futures内置模块。它提供了异步执行任务的高级API

https://docs.python.org/3/library/concurrent.futures.html

下面是修改后的代码。希望能有帮助

import time
import concurrent.futures
from netmiko import ConnectHandler

hosts_info = []
with open('routers.txt', 'r') as devices:
    for line in devices:
        deviceip = line.strip()
        host = {
            'device_type': 'cisco_ios',
            'ip': deviceip,
            'username': 'cisco',
            'password': 'cisco',
            'secret': 'cisco'
        }
        hosts_info.append(host)

starting_time = time.perf_counter()

def open_connection(host):
    try:
        connection = ConnectHandler(**host)

        print('Trying router', host['ip'])
        print('Connection Established to Host:', host['ip'])
        connection.enable()
        sendcommand = connection.send_command('sh run | i hostname')
        return sendcommand
    except:
        print('Connection Failed to host', host['ip'])


with concurrent.futures.ProcessPoolExecutor() as executor:
    results = executor.map(open_connection, hosts_info)

    for result in results:
        print(result)

finish = time.perf_counter()
print('Time Elapsed:', finish - starting_time)

确保在BIOS中启用了超线程设置/多核支持

在遵循@Sandeep的代码之后,我编辑了一些东西,最终实现了主要目标

最后的工作代码在这里

import time
import concurrent.futures
from netmiko import ConnectHandler

hosts_info = []

starting_time = time.perf_counter()

with open('routers.txt', 'r') as devices:
    for line in devices:
        deviceip = line.strip()
        host = {
            'device_type': 'cisco_ios',
            'ip': deviceip,
            'username': 'cisco',
            'password': 'cisco',
            'secret': 'cisco'
        }
        hosts_info.append(host)

def open_connection(host):
    try:
        connection = ConnectHandler(**host)
        print('Connection Established to Host:', host['ip'])
        connection.enable()
        sendcommand = connection.send_command('sh run | i hostname')
        return sendcommand + ' ' + 'For Device:' + host['ip']
    except:
        print('Connection Failed to host', host['ip'])


if __name__ == '__main__':
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = executor.map(open_connection, hosts_info)
        for result in results:
            print(result)

    finish = time.perf_counter()
    print('Time Elapsed:', finish - starting_time)

输出:

Connection Established to Host: 10.10.32.2
Connection Established to Host: 10.10.32.4
Connection Established to Host: 10.10.32.5
Connection Established to Host: 10.10.32.3
hostname R1 For Device:10.10.32.2
hostname R2 For Device:10.10.32.3
hostname R4 For Device:10.10.32.4
hostname R3 For Device:10.10.32.5
Time Elapsed: 7.035125793000589

请注意,ip在引号中应为“ip”。 产量并不是那么高,但现在就可以了。 多谢各位

相关问题 更多 >