1- from pysnmp .hlapi import *
2- from pysnmp .smi import builder , compiler , rfc1902 , view
1+ import asyncio
2+
3+ from pysnmp .hlapi .v1arch .asyncio import (
4+ CommunityData ,
5+ ObjectIdentity ,
6+ ObjectType ,
7+ SnmpDispatcher ,
8+ UdpTransportTarget ,
9+ walk_cmd ,
10+ )
11+ from pysnmp .smi import builder , compiler , error , rfc1902 , view
312
413from server_common .utilities import SEVERITY , print_and_log
514
615# Assemble MIB browser
7- mibBuilder = builder .MibBuilder ()
8- mibViewController = view .MibViewController (mibBuilder )
16+ MIB_BUILDER = builder .MibBuilder ()
17+ MIB_VIEW_CONTROLLER = view .MibViewController (MIB_BUILDER )
918
10- # compiler.addMibCompiler(mibBuilder, sources=['https://mibs.pysnmp.com/asn1/@mib@'], destination='snmp/mibs')
19+ # compiler.addMibCompiler(mibBuilder, sources=['https://mibs.pysnmp.com/asn1/@mib@'],
20+ # destination='snmp/mibs')
1121# The below code needs MIBS to have been downloaded in the below subfolder.
12- compiler .addMibCompiler (mibBuilder , sources = ["file://snmp/mibs" ])
22+ compiler .addMibCompiler (MIB_BUILDER , sources = ["file://snmp/mibs" ])
1323# Pre-load MIB modules we expect to work with
14- mibBuilder . loadModules (
24+ MIB_BUILDER . load_modules (
1525 "SNMPv2-MIB" , "SNMP-COMMUNITY-MIB" , "DISMAN-EXPRESSION-MIB" , "RFC1213-MIB" , "IF-MIB"
1626)
1727
2535]
2636
2737
28- def walk (host , oid , requestedMIBs = INTERESTING_MIBS ):
38+ async def walkasync (
39+ host : str , oid : str , requested_mibs : list [str ] = INTERESTING_MIBS
40+ ) -> dict [str , str ]:
2941 mibmap = dict ()
30- for errorIndication , errorStatus , errorIndex , varBinds in nextCmd (
31- SnmpEngine (),
42+ async for error_indication , error_status , error_index , var_binds in walk_cmd (
43+ SnmpDispatcher (),
3244 CommunityData ("public" , mpModel = 0 ),
33- UdpTransportTarget ((host , 161 ), timeout = 3 , retries = 0 ),
34- ContextData (),
45+ await UdpTransportTarget .create ((host , 161 ), timeout = 3 , retries = 0 ),
3546 ObjectType (ObjectIdentity (oid )),
3647 lookupMib = False ,
3748 lexicographicMode = False ,
3849 lookupNames = True ,
3950 lookupValues = True ,
4051 ):
41- if errorIndication :
52+ if error_indication :
4253 ## we need to look at later - currently will print forever for a moxa that is
4354 ## not on the network. Maybe return status and let caller decide whether to print
4455 # print_and_log(f"Error:: for {host}: {errorIndication}", severity=SEVERITY.MINOR)
4556 break
4657
47- elif errorStatus :
58+ elif error_status :
4859 print_and_log (
49- "host %s: %s at %s"
50- % (
60+ "host {}: {} at {}" .format (
5161 host ,
52- errorStatus . prettyPrint () ,
53- errorIndex and varBinds [int (errorIndex ) - 1 ][0 ] or "?" ,
62+ error_status ,
63+ error_index and var_binds [int (error_index ) - 1 ][0 ] or "?" ,
5464 ),
5565 severity = SEVERITY .MAJOR ,
5666 )
@@ -59,23 +69,28 @@ def walk(host, oid, requestedMIBs=INTERESTING_MIBS):
5969 else :
6070 # Run var-binds through MIB resolver
6171 # You may want to catch and ignore resolution errors here
62- varBinds = [
63- rfc1902 .ObjectType (rfc1902 .ObjectIdentity (x [0 ]), x [1 ]).resolveWithMib (
64- mibViewController
65- )
66- for x in varBinds
67- ]
68- for name , value in varBinds :
72+ res_var_binds = []
73+ for x in var_binds :
74+ try :
75+ y = rfc1902 .ObjectType (rfc1902 .ObjectIdentity (x [0 ]), x [1 ]).resolveWithMib (
76+ MIB_VIEW_CONTROLLER
77+ )
78+ res_var_binds .append ((y [0 ], y [1 ]))
79+ except error .SmiError as e :
80+ print (e )
81+ for name , value in res_var_binds :
6982 mib , exists , port = name .prettyPrint ().partition ("." )
70- if not exists :
71- port = ""
7283 # print(name.prettyPrint(), ' = ', value.prettyPrint())
73- if mib in requestedMIBs :
84+ if mib in requested_mibs :
7485 mibmap [name .prettyPrint ()] = value .prettyPrint ()
7586 # print('MIB-->', mib, ' port-->', port, ' = ', value.prettyPrint())
7687 # print_and_log('MIB -->%s, port --> %s, = %s' % (mib, port, value))
7788
7889 return mibmap
7990
8091
92+ def walk (host : str , oid : str , requested_mibs : list [str ] = INTERESTING_MIBS ) -> dict [str , str ]:
93+ return asyncio .run (walkasync (host , oid , requested_mibs = requested_mibs ))
94+
95+
8196# walk('130.246.49.46', '1.3.6.1.2.1')
0 commit comments