Shorter and more readable code for multiple devices in a file #288
HaiderNL
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The code below works very well.
Just wondering if the for/in loop should be wrapped in a coroutine.
`
import asyncio
from scrapli.driver.core import AsyncIOSXEDriver
with open("addresses.txt") as f1:
f1 = f1.read().splitlines()
device_list = [ ]
should I wrap it in a coroutine ?
for ip in f1:
device = {
"host": ip,
"auth_username": "user1",
"auth_password": "secret1",
"auth_strict_key": False,
"transport": "asyncssh",
}
device_list.append(device)
async def task1(x):
async with AsyncIOSXEDriver(**x) as conn:
output = await conn.send_commands_from_file("commands.txt")
return output
async def main():
coroutines = [task1(y) for y in device_list]
results = await asyncio.gather(*coroutines)
for r in results:
print(r.result)
if name == "main":
asyncio.run(main())
#asyncio.get_event_loop().run_until_complete(main())
`
Beta Was this translation helpful? Give feedback.
All reactions