只走特定的节点

我正在构建一个脚本,该脚本应该通过SNMP(漫游)从调制解调器获取MAC / IP地址。为此,我正在使用PySNMP和nextCmd(asyncore)。尽管我得到了想要的东西,但是目前我得到的比预期的要多:在特定节点上行走之后,它会与剩下的所有其他节点一起继续工作。


代码:


nextCmd(snmpEngine,

        CommunityData('private'),

        UdpTransportTarget(('IP.goes.right.here', 161)),

        ContextData(),

        ObjectType(ObjectIdentity('1.3.6.1.2.1.4.22.1.2')),

        cbFun=cbFun)


snmpEngine.transportDispatcher.runDispatcher()

cbFun


def cbFun(snmpEngine, sendRequestHandle, errorIndication,

      errorStatus, errorIndex, varBindTable, cbCtx):

if errorIndication:

    print(errorIndication)

    return

elif errorStatus:

    print('%s at %s' % (errorStatus.prettyPrint(),

                        errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?'))

    return

else:

    for varBindRow in varBindTable:

        for varBind in varBindRow:

            print(' = '.join([x.prettyPrint() for x in varBind]))


return True 

这纯粹是基于文档和Internet上的其他一些示例。


样本输出:


1.3.6.1.2.1.4.22.1.2.536870914.some.ip.was.here = 0xMacAddress

1.3.6.1.2.1.4.22.1.3.1182728.some.ip.was.here = some.ip.was.here  # next node

1.3.6.1.2.1.4.22.1.4.1182736.some.ip.was.here = 3  # and even further

...

因此,这样做的意图仅限于步行1.3.6.1.2.1.4.22.1.2。


PS我只是从PySNMP开始,我想要类似 snmpwalk -v 2c -c private some.ip.was.here ipNetToPhysicalPhysAddress


狐的传说
浏览 204回答 2
2回答

Cats萌萌

缺少解决此问题的任何内置方法,我对进行了更改cbFun()。现在,它包含一些额外的行,re.search()并且pattern是OID(因此,如果pattern不匹配,即nextCmd转到另一个级别(节点),它就可以了return)。我已经很简单了,因为已经构造了带有response的字符串print(' = '.join([x.prettyPrint() for x in varBind])),所以我已经添加了if条件,并将上面代码的结果分配给了变量,因此使实现此检查成为可能。作为另一种可能的解决方案,也可以使用本示例中描述的方法-使用varBindHead并进行比较。随时添加您的解决方案。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python