Apr-20-2020, 10:31 AM
Hello
It is my first post.
I would like to use asyncio for a network module. The purpose is to send the same request to multiple devices and waiting for the answer from all.
I tried the following code but it is not working.
It is my first post.
I would like to use asyncio for a network module. The purpose is to send the same request to multiple devices and waiting for the answer from all.
I tried the following code but it is not working.
import asyncio from datetime import datetime from jsonrpclib import Server import os, sys, re import time import json from netaddr import * # username and password for the connection username = '' password = '' deviceNotConnected=[] devices =['spine1','spine2','leaf1-A','leaf1-B','leaf2-A','leaf2-B','leaf3-A','leaf4'] commandes=['show lldp neighbors detail', 'show version', 'show interfaces status', 'show ip interface', 'show ip bgp summary'] # Connection to the switch async def openconnexion(device,username,password,cde): switch = Server("http://%s:%s@%s/command-api" %(username,password,device)) try: result = switch.runCmds(version = 1, cmds = cde) except: deviceNotConnected.append(device) pass async def main(): start_time_Final = datetime.now() for device in devices: await asyncio.gather(openconnexion(device,username,password,commandes)) totalFinal = datetime.now() - start_time_Final print (totalFinal) asyncio.run(main())Regards