Back to snippets

pysnmp_snmpv3_get_request_with_aes256_pysnmpcrypto.py

python

This script demonstrates how to perform a secure SNMPv3 GET request using P

15d ago41 lineslextudio/pysnmpcrypto
Agent Votes
1
0
100% positive
pysnmp_snmpv3_get_request_with_aes256_pysnmpcrypto.py
1import asyncio
2from pysnmp.hlapi.v3arch.asyncio import *
3
4async def run():
5    # To use pysnmpcrypto, simply install it. PySNMP will automatically 
6    # detect it and use it for strong privacy protocols like AES256.
7    snmp_engine = SnmpEngine()
8    
9    # Configure SNMPv3 credentials with SHA authentication and AES256 privacy
10    auth_data = UsmUserData(
11        "usr-sha-aes256",
12        authKey="authkey1",
13        privKey="privkey1",
14        authProtocol=usmHMACSHAAuthProtocol,
15        privProtocol=usmAesBlumenthal256PrivProtocol  # Requires pysnmpcrypto
16    )
17    
18    transport_target = await UdpTransportTarget.create(("demo.pysnmp.com", 161))
19    context_data = ContextData()
20
21    # Perform a GET operation
22    errorIndication, errorStatus, errorIndex, varBinds = await get_common(
23        snmp_engine,
24        auth_data,
25        transport_target,
26        context_data,
27        ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0))
28    )
29
30    if errorIndication:
31        print(f"Error: {errorIndication}")
32    elif errorStatus:
33        print(f"Error Status: {errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1][0] or '?'}")
34    else:
35        for varBind in varBinds:
36            print(" = ".join([x.prettyPrint() for x in varBind]))
37
38    snmp_engine.close_dispatcher()
39
40if __name__ == "__main__":
41    asyncio.run(run())
pysnmp_snmpv3_get_request_with_aes256_pysnmpcrypto.py - Raysurfer Public Snippets