[英]Python Multiprocessing - Netmiko
我真的是 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
使用 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)
在遵循@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'。 輸出不是那么花哨,但現在就可以了。 謝謝@https ://stackoverflow.com/users/4087758/sandeep-nagendra
確保在 BIOS 中啟用了超線程設置/多核支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.