I'm trying to poll a large number of devices using aiosnmp/asyncio. I'm not sure what I am doing wrong. I'd like to feed a list of IP subnets to the "loop_over_ips" function. Then use the ip address module to loop over every single IP address and add the that async "poll" function to the asyncio.gather pool.
Seems I've got two problems here. First problem is that I'm not getting any responses. The "res.value" is coming back as None even though devices in this range should be responding. Second problem is that at a certain point the application is throwing a runtimeerror saying "cannot reuse already awaited coroutine".
Two questions then, how might I rewrite these functions to avoid the reuse of an awaited coroutine? Have I written these functions improperly leading to no devices responding? With my code the way it is, is it even polling these devices?
import asyncio
import aiosnmp
import ipaddress
async def loop_over_ips(ip_addr_list: list) -> asyncio.gather():
tasks = []
for ip_addr in ip_addr_list:
subnet = ipaddress.ip_network(ip_addr)
for ip in subnet:
ip = str(ip)
tasks.append(poll(ip))
await asyncio.gather(*tasks)
async def poll(ip: str):
with aiosnmp.Snmp(host=ip, community="Mycommunity") as snmp:
try:
for res in await snmp.get(".3.6.1.2.1.1.1.0"):
# for res in await snmp.walk():
print(res.oid, res.value)
except Exception:
# print("Device did not respond")
pass
if __name__ == "__main__":
infra_subnets = [
"10.204.88.0/21",
"10.201.7.0/24",
"172.30.200.0/23",
"172.30.210.0/23",
"10.0.48.0/24",
"10.1.20.0/24",
"10.1.30.0/24",
"10.1.32.0/24",
"10.19.1.0/24",
"10.98.1.0/24",
]
asyncio.run(loop_over_ips(infra_subnets))