網工Python——netdev(異步並行),舉例看看ssh多臺交換機執行命令異步和同步耗時對比實驗:css
代碼簡介:ssh兩臺華爲交換機10.30.0.1和10.30.8.3,執行dis vlan summary,以後結算同步和異步耗時對比!python
ip_list.txt文件內容是ios
同步方式的代碼sshSW_tongbu.py,耗時40.34秒bash
from netmiko import ConnectHandler import time f = open('ip_list.txt') start_time = time.time() for ips in f.readlines(): ip = ips.strip() SW = { 'device_type': 'huawei', 'ip': ip, 'username': 'admin@123', 'password': 'password123', #填寫你的交換機密碼 } connect = ConnectHandler(**SW) print ("Sucessfully connected to " + SW['ip']) config_commands = ['dis vlan summary','quit'] output = connect.send_config_set(config_commands) print (output) print ('Time elapsed: %.2f'%(time.time()-start_time))
執行結果:
app
PS D:\Python_Files\python2020item\netdev_demo> python.exe .\sshSW_tongbu.py Sucessfully connected to 10.30.0.1 system-view Enter system view, return user view with Ctrl+Z. [S7706]dis vlan summary Static VLAN: Total 58 static VLAN. 1 3 10 37 50 to 55 80 to 85 101 to 116 201 to 203 300 400 to 402 801 to 805 900 999 to 1001 2001 2032 to 2039 4000 Dynamic VLAN: Total 0 dynamic VLAN. Reserved VLAN: Total 0 reserved VLAN. [S7706]quit <S7706> Sucessfully connected to 10.30.8.3 system-view Enter system view, return user view with Ctrl+Z. [S5700-3]dis vlan summary Static vlan: Total 25 static vlan. 1 50 to 54 101 to 115 1000 to 1001 1314 to 1315 Dynamic vlan: Total 0 dynamic vlan. Reserved vlan: Total 0 reserved vlan. [S5700-3]quit <S5700-3> Time elapsed: 40.34 PS D:\Python_Files\python2020item\netdev_demo>
異步方式的代碼sshSW_tongbu.py,耗時12.69秒ssh
import asyncio import netdev import time async def task(dev): async with netdev.create(**dev) as ios: commands = ['dis vlan summary','quit'] out = await ios.send_config_set(commands) print(out) async def run(): devices = [] f = open('ip_list.txt') for ips in f.readlines(): ip = ips.strip() dev = { 'username' : 'admin@123', 'password' : 'password123', #填寫你的交換機密碼 'device_type': 'hp_comware', #華爲 華三測試用這個type,由於沒有huawei huasan關鍵字的type,netdev目前2020年僅支持它羅列的操做系統 'host': ip } devices.append(dev) tasks = [task(dev) for dev in devices] await asyncio.wait(tasks) start_time = time.time() asyncio.run(run()) print ('Time elapsed: %.2f'%(time.time()-start_time)) ''' 備註:netdev目前2020年僅支持它羅列的操做系統 ValueError: Unsupported device_type: currently supported platforms are: arista_eos aruba_aos_6 aruba_aos_8 cisco_asa cisco_ios cisco_ios_xe cisco_ios_xr cisco_nxos fujitsu_switch hp_comware hp_comware_limited hw1000 juniper_junos mikrotik_routeros terminal ubiquity_edge '''
執行結果:
異步
PS D:\Python_Files\python2020item\netdev_demo> python.exe .\sshSW_yibu.py C:\Python38\lib\site-packages\asyncssh\crypto\ec.py:143: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point pub = ec.EllipticCurvePublicNumbers.from_encoded_point(curve(), system-view Enter system view, return user view with Ctrl+Z. [ZD-CO-S7706]dis vlan summary Static VLAN: Total 58 static VLAN. 1 3 10 37 50 to 55 80 to 85 101 to 116 201 to 203 300 400 to 402 801 to 805 900 999 to 1001 2001 2032 to 2039 4000 Dynamic VLAN: Total 0 dynamic VLAN. Reserved VLAN: Total 0 reserved VLAN. [S7706]quit <S7706> system-view Enter system view, return user view with Ctrl+Z. [S7706]dis vlan summary Static VLAN: Total 58 static VLAN. 1 3 10 37 50 to 55 80 to 85 101 to 116 201 to 203 300 400 to 402 801 to 805 900 999 to 1001 2001 2032 to 2039 4000 Dynamic VLAN: Total 0 dynamic VLAN. Reserved VLAN: Total 0 reserved VLAN. [S7706]quit <S7706> system-view Enter system view, return user view with Ctrl+Z. [S5700-3]dis vlan summary Static vlan: Total 25 static vlan. 1 50 to 54 101 to 115 1000 to 1001 1314 to 1315 Dynamic vlan: Total 0 dynamic vlan. Reserved vlan: Total 0 reserved vlan. [S5700-3]quit <S5700-3> Time elapsed: 12.69 PS D:\Python_Files\python2020item\netdev_demo>
能夠看出多臺設備ssh並操做,異步方式耗時少不少,因此之後多用用異步,提升效率!!!
async
記得安裝netdev,此外我看見安裝netdev時候安裝了asyncssh庫,其實你看asyncssh的async的英文就是異步意思,就大概知道netdev有處理異步工做的相關能力了!ide