Python 如何从pysnmp中捕获的var绑定中正确获取表行索引和命名值
我试图保持我的代码尽可能干净,但我对目前为止所取得的成绩并不完全满意 我构建了一个SNMP管理器,它使用自定义MIB(我将称之为MY-MIB)从另一个设备接收陷阱 我不确定这是最干净的方式,但基本上我有:Python 如何从pysnmp中捕获的var绑定中正确获取表行索引和命名值,python,python-3.x,pysnmp,Python,Python 3.x,Pysnmp,我试图保持我的代码尽可能干净,但我对目前为止所取得的成绩并不完全满意 我构建了一个SNMP管理器,它使用自定义MIB(我将称之为MY-MIB)从另一个设备接收陷阱 我不确定这是最干净的方式,但基本上我有: from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp from pysnmp.entity.rfc3413 import ntfrcv, context from pysnm
from pysnmp.entity import engine, config
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv, context
from pysnmp.smi import builder, rfc1902
from pysnmp.smi.view import MibViewController
from pysnmp.entity.rfc3413 import mibvar
_snmp_engine = engine.SnmpEngine()
_snmpContext = context.SnmpContext(_snmpEngine)
_mibBuilder = _snmpContext.getMibInstrum().getMibBuilder()
#Add local path where MY-MIB is located
_mibSources = _mibBuilder.getMibSources() + (builder.DirMibSource('.'),)
_mibBuilder.setMibSources(*mibSources)
_mibBuilder.loadModules('MY-MIB')
_view_controller = MibViewController(_mibBuilder)
def my_callback_trap_processor(snmp_engine, state_reference,
context_id, context_name, var_binds, ctx):
#...CALLBACK CODE...
config.addV1System(snmp_engine, 'my-area', 'MYCOMMUNITY')
config.addTargetParams(snmp_engine, 'my-creds', 'my-area',
'noAuthNoPriv', 1)
config.addSocketTransport(snmp_engine,
udp.domainName + (1,),
udp.UdpTransport().openServerMode((IP_ADDRESS,
PORT)))
ntfrcv.NotificationReceiver(snmp_engine, my_callback_trap_processor)
snmp_engine.transportDispatcher.jobStarted(1)
try:
snmp_engine.transportDispatcher.runDispatcher()
except:
snmp_engine.transportDispatcher.closeDispatcher()
raise
在上面的回调函数中,我只需使用以下代码就可以得到一个非常清晰的打印:
varBinds = [rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(_view_controller) for x in var_binds]
for varBind in varBinds:
print(varBind.prettyPrint())
从我所受的陷阱中,我得到:
SNMPv2-MIB::sysUpTime.0 = 0
SNMPv2-MIB::snmpTrapOID.0 = MY-MIB::myNotificationType
MY-MIB::myReplyKey.47746."ABC" = 0x00000000000000000000000000000000000
MY-MIB::myTime.0 = 20171115131544Z
MY-MIB::myOperationMode.0 = 'standalone'
sysUpTime SNMPv2-MIB (Integer(0),) 0
snmpTrapOID SNMPv2-MIB (Integer(0),) 1.3.6.1.X.Y.Z.A.B.C.D
myReplyKey MY-MIB (myTimeStamp(47746), myName(b'X00080')) 0x00000000000000000000000000000000000
myTime MY-MIB (Integer(0),) 20171115131544Z
myOperationMode MY-MIB (Integer(0),) 1
很好。但我想操纵/剖析给定var绑定中的每一位信息,特别是以更高级别的方式
通过查看库的内部,我能够收集到以下代码:
for varBind in var_binds:
objct = rfc1902.ObjectIdentity(varBind[0]).resolveWithMib(self._view_controller)
(symName, modName), indices = mibvar.oidToMibName(
self._view_controller, objct.getOid()
)
print(symName, modName, indices, varBind[1])
这给了我:
SNMPv2-MIB::sysUpTime.0 = 0
SNMPv2-MIB::snmpTrapOID.0 = MY-MIB::myNotificationType
MY-MIB::myReplyKey.47746."ABC" = 0x00000000000000000000000000000000000
MY-MIB::myTime.0 = 20171115131544Z
MY-MIB::myOperationMode.0 = 'standalone'
sysUpTime SNMPv2-MIB (Integer(0),) 0
snmpTrapOID SNMPv2-MIB (Integer(0),) 1.3.6.1.X.Y.Z.A.B.C.D
myReplyKey MY-MIB (myTimeStamp(47746), myName(b'X00080')) 0x00000000000000000000000000000000000
myTime MY-MIB (Integer(0),) 20171115131544Z
myOperationMode MY-MIB (Integer(0),) 1
对于myReplyKey索引,我只需执行以下操作:
for idx in indices:
try:
print(idx.getValue())
except AttributeError:
print(int(idx))
但是在
myOperationMode
var绑定的情况下,如何获得命名值'standalone'
,而不是1
?以及如何获取索引的名称(myTimeStamp
和myName
)
更新:
在Ilya的建议之后,我对这个库进行了更多的研究,以获得namedvalue,而且,我还使用了一些Python黑客技术来获得我在索引上所寻找的内容
varBinds = [rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(_view_controller) for x in var_binds]
processed_var_binds = []
for var_bind in resolved_var_binds:
object_identity, object_value = var_bind
mod_name, var_name, indices = object_identity.getMibSymbol()
var_bind_dict = {'mib': mod_name, 'name': var_name, 'indices': {}}
for idx in indices:
try:
value = idx.getValue()
except AttributeError:
var_bind_dict['indices'] = int(idx.prettyPrint())
else:
var_bind_dict['indices'][type(value).__name__] = str(value)
try:
var_bind_dict['value'] = object_value.namedValues[object_value]
except (AttributeError, KeyError):
try:
var_bind_dict['value'] = int(object_value.prettyPrint())
except ValueError:
var_bind_dict['value'] = object_value.prettyPrint()
processed_var_binds.append(var_bind_dict)
要针对MIB解析SNMP PDU var绑定,您可以使用我认为您已经完成的操作:
from pysnmp.smi.rfc1902 import *
var_binds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController)
for x in var_binds]
至此,您已经有了一个对象列表。ObjectType
实例模拟两元素元组:and
现在,将为您提供MIB名称、MIB对象名称和由OID尾部组成的索引元组。索引元素与对象\u值一样
:
>>> object_identity.getMibSymbol()
('SNMPv2-MIB', 'sysDescr', (0,))
枚举(如果存在)由报告。prettyPrint()
:
请尝试
varBind[1]。prettyPrint()
以获取命名值。虽然我不确定你解剖的最终目的是什么。您想获得MIB名称、MIB对象和可能的索引吗?我已经尝试过了,但实际上它抱怨缺少一个位置参数。因此,我甚至尝试了varBind[1]。prettyPrint(varBind[1])
仍然提供了1
以及如何在循环时获取索引的名称(myTimeStamp
和myName
)?@Ilya“你想获得MIB名称、MIB对象和可能的索引吗?”是的,以及存在时的值和命名值。