网工Python---netdev(异步并行),举例看看ssh多台交换机执行命令异步和同步耗时对比

网工Python——netdev(异步并行),举例看看ssh多台交换机执行命令异步和同步耗时对比实验:

代码简介:ssh两台华为交换机10.30.0.1和10.30.8.3,执行dis vlan summary,之后结算同步和异步耗时对比!

ip_list.txt文件内容是

image.png

同步方式的代码sshSW_tongbu.py,耗时40.34秒

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))

执行结果:

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秒

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并操作,异步方式耗时少很多,所以以后多用用异步,提高效率!!!

记得安装netdev,此外我看见安装netdev时候安装了asyncssh库,其实你看asyncssh的async的英文就是异步意思,就大概知道netdev有处理异步工作的相关能力了!

猜你喜欢

转载自blog.51cto.com/8189171/2513025