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

问题描述 投票:0回答:1

我是SNMP的新手,我正在尝试在云中创建SNMP代理,该代理将使用上下文名称来区分设备。我在代理端使用pysnmp。我还遵循以下示例程序http://snmplabs.com/pysnmp/examples/contents.html

现在,我想知道如何在代理端添加注册多个上下文名称并在此上下文名称下注册MIB。全部使用相同的非默认MIB。

我尝试了www.snmpalabs.com上可用的任何示例代码,但是当我使用上下文名称时,会得到Time Out或MId的ENd。

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)

我只粘贴了最少的代码。请询问是否需要更多。这不是正确的方法吗?为此是否有任何好的文档或帮助?

python snmp agent pysnmp
1个回答
0
投票

基本上,您应该有一个SnmpContext对象和多个MIB树,每个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)

然后您应该能够像这样查询每个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

由于MIB树为空,因此您可能会出现mib结束。

希望这里是可操作的example script

© www.soinside.com 2019 - 2024. All rights reserved.