简体   繁体   English

如何在不同的 (SNMPv3) 上下文中注册 MIB 模块。 在 SNMP 代理端

[英]How can I register a MIB module in a different (SNMPv3) context. In SNMP agent side

I am new to SNMP and I am trying to create SNMP agent in the cloud which will use the context name to distinguish the devices.我是 SNMP 新手,我正在尝试在云中创建 SNMP 代理,它将使用上下文名称来区分设备。 I am using pysnmp in the agent side.我在代理端使用 pysnmp。 I am also following the sample program available in the http://snmplabs.com/pysnmp/examples/contents.html我也在关注http://snmplabs.com/pysnmp/examples/contents.html中提供的示例程序

Now I want to know how to add register multiple context name in the agent side and register MIB under this context name.现在我想知道如何在代理端添加注册多个上下文名称并在此上下文名称下注册MIB。 All using the same non-default MIB.全部使用相同的非默认 MIB。

I tried whatever sample codes available in www.snmpalabs.com, But when i use the context name I get Time Out or ENd Of MIB.我尝试了 www.snmpalabs.com 中提供的任何示例代码,但是当我使用上下文名称时,我得到 Time Out 或 ENd Of MIB。

def __init__(self, mibObjects):

 snmpEngine = engine.SnmpEngine()

 config.addTransport(
      self._snmpEngine, udp.domainName,                              
      udp.UdpTransport().openServerMode(('127.0.0.1', 161)))

 config.addV3User(self._snmpEngine, 'User', config.usmHMACMD5AuthProtocol, 'PassCode')

 config.addVacmUser(self._snmpEngine, 3, 'User', 'authNoPriv', (1, 3, 6, 1, 4, 1, 44555), (1, 3, 6, 1, 4, 1, 44555))

 snmpContext = context.SnmpContext(snmpEngine)

        responder(snmpEngine,snmpContext)

 mibBuilder = snmpContext.getMibInstrum().getMibBuilder()

 loadmib(mibBuilder)

 snmpContext.registerContextName(
                 v2c.OctetString('MyContextName'),
                 instrum.MibInstrumController(mibBuilder)
        )

 MibScalarInstance, = mibBuilder.importSymbols('SNMPv2-SMI', 'MibScalarInstance')

 # export our custom mib
 for mibObject in mibObjects:
       nextVar, = mibBuilder.importSymbols(mibObject.mibName,                                
       mibObject.objectType)
       instance = createVariable(MibScalarInstance,          
                                mibObject.valueFunc,                                     
                                mibObject.objectType,                                    
                                nextVar.name, (0,),                                  
                                nextVar.syntax)
      instanceDict = {str(nextVar.name) + "Instance":instance}
      mibBuilder.exportSymbols(mibObject.mibName,
                                        **instanceDict)

I just pasted only minimal code.我只粘贴了最少的代码。 Please ask if need more.请询问是否需要更多。 Is it not the right way, then what is it?这不是正确的方法,那么它是什么? Is there any good documentation or help available for this?是否有任何好的文档或帮助可用?

Essentially, you should have one SnmpContext object and multiple MIB trees each registered with SnmpContext under some distinct name.本质上,您应该拥有一个SnmpContext object 和多个 MIB 树,每个树都以某个不同的名称注册到SnmpContext

# Create an SNMP context with default ContextEngineId (same as SNMP engine ID)
snmpContext = context.SnmpContext(snmpEngine)

# Create multiple independent trees of MIB managed objects (empty so far)
mibTreeA = instrum.MibInstrumController(builder.MibBuilder())
mibTreeB = instrum.MibInstrumController(builder.MibBuilder())

# Register MIB trees at distinct SNMP Context names
snmpContext.registerContextName(v2c.OctetString('context-a'), mibTreeA)
snmpContext.registerContextName(v2c.OctetString('context-b'), mibTreeB)

# Register SNMP Applications at the SNMP engine for particular SNMP context
cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)

Then you should be able to query each of the MIB trees like this:然后您应该能够像这样查询每个 MIB 树:

snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 -n context-a 127.0.0.1 .1.3.6
snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 -n context-b 127.0.0.1 .1.3.6

You will likely get just end-of-mib because MIB trees are empty.由于 MIB 树是空的,您可能会得到只是 end-of-mib。

Here is a hopefully operational example script .这是一个可操作的示例脚本

Alternatively to implementing your own SNMP agent from the scratch, you might consider trying out SNMP Command Responder tool based on the same technology, but making it easier (hopefully).或者从头开始实现自己的 SNMP 代理,您可能会考虑尝试基于相同技术的SNMP 命令响应工具,但要使其更容易(希望如此)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM