简体   繁体   English

Pysnmp:SnmpWalk从下一个oid开始

[英]Pysnmp: SnmpWalk starts on the next oid

I want to start walking from the oid that i provided to NextCmd and not from the oid after that. 我想从我提供给NextCmd的oid开始,而不是从那之后的oid开始。 Here is my code: 这是我的代码:

import datetime, time
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import builder, view, error

def __cbFun_Walk(sendRequestHandle, errorIndication, errorStatus, errorIndex, varBindTable, cbCtx):
    (authData, transportTarget) = cbCtx
    for varBindTableRow in varBindTable:
        for oid, val in varBindTableRow:
            print 'OID: ' + str(oid) + ' VAL: ' + str(val.prettyPrint())
    return False

cmdGen = cmdgen.AsynCommandGenerator()
cmdGen.asyncNextCmd(
        cmdgen.CommunityData('public', mpModel=1),
        cmdgen.UdpTransportTarget(('192.168.0.101', 161)),
        (str('1.3.6.1.2.1.4.1.0'),),
        (__cbFun_Walk, (cmdgen.CommunityData('public', mpModel=1), cmdgen.UdpTransportTarget(('192.168.0.101', 161)))))
cmdGen.snmpEngine.transportDispatcher.runDispatcher() 

This code prints "OID: 1.3.6.1.2.1.4.2.0 VAL: 64". 此代码显示“ OID:1.3.6.1.2.1.4.2.0 VAL:64”。
As you can see it gives me the value of the oid that comes after the oid that i provided. 如您所见,它为我提供了我提供的oid之后的oid值。
I would like to get the value of the oid "1.3.6.1.2.1.4.1.0" using NextCmd. 我想使用NextCmd获得oid“ 1.3.6.1.2.1.4.1.0”的值。

What you observe is exactly how GETNEXT/GETBULK SNMP commands should work. 您所观察到的正是GETNEXT / GETBULK SNMP命令应如何工作。 If you want to fetch one specific OID, you should use the GET command along with the GETNEXT iteration. 如果要获取一个特定的OID,则应将GET命令与GETNEXT迭代一起使用。

In other words, you could have two-phase script - first you run asyncGetCmd() against the OID you want to read. 换句话说,您可能有两个阶段的脚本-首先,对要读取的OID运行asyncGetCmd()。 Then you fire up asyncNextCmd() against the same OID. 然后,您针对相同的OID启动asyncNextCmd()。 For both steps you can use nearly identical code from your example. 对于这两个步骤,您都可以使用示例中几乎相同的代码。 Ultimately, you fetch both initial OID and all subsequent ones. 最终,您将同时获取初始OID和所有后续OID。

Alternatively, a hackerish approach, would be to artificially "decrement" the OID you aim at so that the "next" OID to the "decremented" one would become precisely the OID you want to read. 或者,一种骇人听闻的方法是人为地“递减”您要瞄准的OID,以便“递减”的OID变成“您要阅读的OID”。 And here you have two options: either you can strip the last sub-OID (eg you end up with 1.3.6.1.2.1.4.1) or decrement the rightmost greater-then-zero sub-OID (eg 1.3.6.1.2.1.4.0.0). 在这里,您有两个选择:要么可以剥离最后一个子OID(例如,最终得到1.3.6.1.2.1.4.1),要么递减最右边的零个子OID(例如1.3.6.1.2.1)。 4.0.0)。

The problem here is that generally you can't be absolutely sure that no OIDs fall in-between. 这里的问题是,通常您不能绝对确定没有OID介于两者之间。 Then you may end-up getting the wrong OID in response. 然后,您可能最终得到错误的OID作为响应。 However stripping the last '0' is probably save (since it's a scalar). 但是,删除最后一个“ 0”可能会节省(因为它是标量)。

As a side note: consider trying out the latest high-level APU , it might be a little easier to deal with. 附带说明:考虑试用最新的高级APU ,可能会更容易处理。

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

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