为了账号安全,请及时绑定邮箱和手机立即绑定

只走特定的节点

只走特定的节点

狐的传说 2021-05-07 14:27:20
我正在构建一个脚本,该脚本应该通过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()cbFundef cbFun(snmpEngine, sendRequestHandle, errorIndication,      errorStatus, errorIndex, varBindTable, cbCtx):if errorIndication:    print(errorIndication)    returnelif errorStatus:    print('%s at %s' % (errorStatus.prettyPrint(),                        errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?'))    returnelse:    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 = 0xMacAddress1.3.6.1.2.1.4.22.1.3.1182728.some.ip.was.here = some.ip.was.here  # next node1.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
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

缺少解决此问题的任何内置方法,我对进行了更改cbFun()。现在,它包含一些额外的行,re.search()并且pattern是OID(因此,如果pattern不匹配,即nextCmd转到另一个级别(节点),它就可以了return)。我已经很简单了,因为已经构造了带有response的字符串print(' = '.join([x.prettyPrint() for x in varBind])),所以我已经添加了if条件,并将上面代码的结果分配给了变量,因此使实现此检查成为可能。

作为另一种可能的解决方案,也可以使用示例中描述的方法-使用varBindHead并进行比较。

随时添加您的解决方案。


查看完整回答
反对 回复 2021-05-11
  • 2 回答
  • 0 关注
  • 203 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号