我真的是 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