Python 仅漫游特定节点
我正在构建一个脚本,该脚本应该通过SNMP(WACK)从调制解调器获取MAC/IP地址。为此,我使用PySNMP和Python 仅漫游特定节点,python,snmp,pysnmp,Python,Snmp,Pysnmp,我正在构建一个脚本,该脚本应该通过SNMP(WACK)从调制解调器获取MAC/IP地址。为此,我使用PySNMP和nextCmd(asyncore)。虽然我得到了我想要的,但目前我得到的比我预期的要多:在遍历特定节点之后,它将继续处理剩下的所有其他节点 代码: nextCmd(snmpEngine, CommunityData('private'), UdpTransportTarget(('IP.goes.right.here', 161)),
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()
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
...
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
这纯粹是基于互联网上发现的一些其他例子
样本输出:
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()
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
另外,我刚开始使用PySNMP,我想要
snmpwalk-v 2c-c private some.ip.was.here ipNetToPhysicalPhysAddress
您是否考虑过将词典编纂代码添加到下一个TCMD属性中
比如:
nextCmd(snmpEngine,
CommunityData('private'),
UdpTransportTarget(('IP.goes.right.here', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.4.22.1.2')),lexicographicMode=False,
cbFun=cbFun)
由于没有任何内置方法来解决此问题,我对
cbFun()
进行了更改。现在,它有一些额外的行,其中包含re.search()
,并且模式是OID(因此,当模式不匹配时,即nextCmd转到另一个级别(节点),它只返回即可)。我已经使它变得简单,并且已经用responseprint('='.join([x.prettyPrint()表示varBind中的x])
构造了字符串,因此我添加了if
条件并将上述代码的结果赋给了一个变量,所以我可以实现这个检查
作为另一种可能的解决方案,也可以使用示例中描述的方法——使用varBindHead并比较它们
请随意添加您的解决方案。考虑解释一下little@Pieter据我所知,这在异步版本的nextCmd中是不可能的。