错误:OID没有增加:SNMPv2-SMI :: dod,如何增加OID?

问题描述 投票:2回答:2

我正在尝试使用两个不同的上下文名称来实现snmp代理。我得到了一些示例程序。这是我的代理端代码,如下所示。这是我从snmplabs.com获得的。我尝试实现它,并且在执行snmpwalk时遇到一些错误。

"""
| $ 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

"""  #
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.smi import instrum, builder
from pysnmp.proto.api import v2c
import datetime
from pysnmp.smi import exval
# Create SNMP engine
snmpEngine = engine.SnmpEngine()

# Transport setup

# UDP over IPv4
config.addTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpTransport().openServerMode(('127.0.0.1', 161))
)

# SNMPv3/USM setup

# user: usr-md5-none, auth: MD5, priv NONE
config.addV3User(
    snmpEngine, 'usr-md5-none',
    config.usmHMACMD5AuthProtocol, 'authkey1'
)

# Allow full MIB access for each user at VACM
config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))

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


class EchoMibInstrumController(instrum.AbstractMibInstrumController):

    def readVars(self, varBinds, acInfo=(None, None)):
        retItem = []
        print ('varbinds', varBinds)
        for ov in varBinds:
            if str(ov[0]) == '1.3.6.1.2.1.1.1.0':
                currentDT = datetime.datetime.now()
                retItem.extend([(ov[0], v2c.OctetString('Hello World! It\'s currently: %s' % str(currentDT)))])
            elif str(ov[0]) == '1.3.6.1.2.1.1.1.1': 
                retItem.extend([(ov[0], v2c.OctetString('You queried  walk  OID %s' % ov[0]))])
            else:
                retItem.extend([(ov[0], v2c.OctetString('You queried readVars OID %s' % str(currentDT)))])
        return retItem

    def readNextVars(self, varBinds, acInfo=(None, None)):
        retItem = []
        print ('Next varbinds', varBinds)
        for ov in varBinds:
            if str(ov[0]) == '1.3.6.1.2.1.1.1.0':
                currentDT = datetime.datetime.now()
                retItem.extend([(ov[0], v2c.OctetString('Hello World! It\'s currently: %s' % str(currentDT)))])
            elif str(ov[0]) == '1.3.6.1.2.1.1.1.1': 
                retItem.extend([(ov[0], v2c.OctetString('You queried  walk  OID %s' % ov[0]))])
            else:
                currentDT = datetime.datetime.now()
                retItem.extend([(ov[0], v2c.OctetString('You queried readNextVars OID %s' % str(currentDT)))])
        return retItem


mibBuilder = snmpContext.getMibInstrum().getMibBuilder()
# mibInstrum = instrum.MibInstrumController(mibBuilder)

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


class MyStaticMibScalarInstance(MibScalarInstance):

    def getValue(self, name, idx):
        currentDT = datetime.datetime.now()
        return self.getSyntax().clone(
            'Hello World! It\'s currently: ' + str(currentDT)
        )


mibBuilder.exportSymbols(
    '__MY_MIB', MibScalar((1, 3, 6, 1, 2, 1, 1, 1), v2c.OctetString()),
    MyStaticMibScalarInstance((1, 3, 6, 1, 2, 1, 1, 1), (0,), v2c.OctetString())
)

# Create multiple independent trees of MIB managed objects (empty so far)
mibTreeA = EchoMibInstrumController()
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)

oid, val = (), None
# logging.debug('done')

# 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)

# Register an imaginary never-ending job to keep I/O dispatcher running forever
snmpEngine.transportDispatcher.jobStarted(1)

# Run I/O dispatcher which would receive queries and send responses
try:
    snmpEngine.transportDispatcher.runDispatcher()

except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise

[当我做snmpwalk之类的时候

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

给出类似答复

SNMPv2-SMI::dod = STRING: "You queried readNextVars OID 2019-11-21 19:18:22.566000"
Error: OID not increasing: SNMPv2-SMI::dod
 >= SNMPv2-SMI::dod

所以我的疑问是我在做什么错以及如何增加此OID?

python snmp pysnmp mib
2个回答
0
投票

根据手册页:

选项

-Cc不要检查返回的OID是否正在增加。某些代理(例如,LaserJets)会按顺序返回OID,但仍然可以完成步行。其他代理返回的OID为可能会导致snmpwalk无限循环。默认情况下,snmpwalk尝试检测此行为并碰到代理人非法行事时会警告您。使用-Cc关闭这个检查。

这似乎是默认行为,但绝不强制执行


0
投票

您的服务器(代理)应返回的OID永远不要少于GETNEXT / GETBULK命令所到达的OID。

使用您的代码,确保readNextVars始终返回递增的OID。

SNMP管理器对此条件进行了检查,否则管理器-代理对可能参与无休止的交换。

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