-->

Reuse getCmd object in pysnmp

2019-09-14 03:56发布

问题:

In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the getCmd class exposes methods to allow me to change the oids.

http://pysnmp.sourceforge.net/docs/hlapi/asyncore/sync/manager/cmdgen/getcmd.html

回答1:

The getCmd name is referring to a function, not a class. Technically, it is a generator, but that is not important here.

It is cheap to call *Cmd() because all the heavy lifting and state management is done on the SnmpEngine object (first argument to getCmd). Thus it is important, from performance standpoint, to keep SnmpEngine object as persistent as possible.

>>> from pysnmp.hlapi.asyncore import *
>>> snmpEngine = SnmpEngine()
>>> for oid in ['1.3.6.1.2.1.2.2.1.8.1', '1.3.6.1.2.1.2.2.1.8.2']:
...     g = getCmd(snmpEngine,
...                CommunityData('public'),
...                UdpTransportTarget(('demo.snmplabs.com', 161)),
...                ContextData(),
...                ObjectType(ObjectIdentity(oid)))
>>>     print(next(g))