hi friends,
this is my code.
from flask import Flask, render_template
import asyncio
from pysnmp.hlapi.v3arch.asyncio import *
app = Flask(name)
Define your switches and their SNMP details
SWITCHES = [
{“name”: “Switch 1”, “ip”: “10.2.200.49”, “community”: “public”},
{“name”: “Switch 2”, “ip”: “10.2.201.195”, “community”: “public”},
{“name”: “Switch 3”, “ip”: “10.2.20.195”, “community”: “public”},
]
SNMP OIDs for additional information
OID_SYSNAME = ‘1.3.6.1.2.1.1.5.0’ # System Name OID
OID_SERIAL = ‘1.3.6.1.2.1.47.1.1.1.1.11.1’ # Serial Number (example, vendor-specific)
OID_FIRMWARE = ‘1.3.6.1.2.1.47.1.1.1.1.10.1’ # Firmware version (example, vendor-specific)
async def get_snmp_data_async(ip, community, oid):
“”“Asynchronously query SNMP data from a switch.”“”
try:
snmpEngine = SnmpEngine()
iterator = get_cmd(
snmpEngine,
CommunityData(community),
await UdpTransportTarget.create((ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
)
error_indication, error_status, error_index, var_binds = await iterator
if error_indication:
return f"Error: {error_indication}"
elif error_status:
return f"Error: {error_status.prettyPrint()}"
else:
for var_bind in var_binds:
return f"{var_bind[1]}"
except Exception as e:
return f"Error: {e}"
@app.route(“/”)
async def index():
tasks =
for switch in SWITCHES:
tasks.append(
asyncio.gather(
get_snmp_data_async(switch[“ip”], switch[“community”], OID_SYSNAME),
get_snmp_data_async(switch[“ip”], switch[“community”], OID_SERIAL),
get_snmp_data_async(switch[“ip”], switch[“community”], OID_FIRMWARE),
)
)
# Execute all SNMP queries concurrently
results = await asyncio.gather(*tasks)
# Create statuses for rendering
statuses = []
for switch, result in zip(SWITCHES, results):
sysname, serial, firmware = result
statuses.append(
{
"name": switch["name"],
"ip": switch["ip"],
"sysname": sysname,
"serial": serial,
"firmware": firmware,
}
)
return render_template("index.html", switches=statuses)
if name == “main”:
app.run(host=“0.0.0.0”, port=5000, debug=True)
and out put for the same serial number firmware not displaying