简体   繁体   English

pysnmp snmp获取托管对象

[英]pysnmp snmp get with managed objects

I'm trying to figure out how to load mibs and get the sysObjectID of a cisco switch and then resolve it into something human readable. 我试图弄清楚如何加载mib并获取cisco交换机的sysObjectID,然后将其解析为人类可读的内容。 Right now my code works but what is confusing me is are the arguments for getCmd(), instead of sending a string with OID how do I send a managed object? 现在,我的代码可以工作了,但令我感到困惑的是getCmd()的参数,而不是发送带有OID的字符串,如何发送托管对象?

how can I change the following line in my code 如何更改代码中的以下行

'1.3.6.1.2.1.1.2.0',

To be something like 'sysObjectdID.0' but as a managed object 类似于“ sysObjectdID.0”,但作为托管对象

#!/bin/env python
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import view
from pysnmp.smi.rfc1902 import ObjectType, ObjectIdentity

cmdGen = cmdgen.CommandGenerator()
print "Loading & Indexing MIB Modules..."
mibBuilder = cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
mibPath = mibBuilder.getMibPath() + ('/Users/jeffrey.dambly/Documents/mibs',)
mibBuilder.setMibPath(*mibPath)
cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.loadModules('CISCO-SMI', 'CISCO-PRODUCTS-MIB')

mibView = view.MibViewController(mibBuilder)

print 'Done'
#print 'MIB symbol name lookup by name: '
#oid, label, suffix = mibView.getNodeName((1, 3, 6, 1, 4, 1, 9, 1, 12))
#print oid, label, suffix
print 'Done'

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.CommunityData('xxxxxxxx'),
    cmdgen.UdpTransportTarget(('192.168.8.10', 161)),
    '1.3.6.1.2.1.1.2.0',
    lookupNames=True,
    lookupValues=True,
)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

If you are using pysnmp 4.2.*, pass your MIB object to the MibVariable class instance. 如果您使用的是pysnmp 4.2。*,请将您的MIB对象传递给MibVariable类实例。

Or better use pysnmp 4.3 where ObjectType/ObjectIdentity classes are used for this purpose. 或者更好地使用pysnmp 4.3,其中将ObjectType / ObjectIdentity类用于此目的。

Example code (pysnmp 4.3): 示例代码(pysnmp 4.3):

getCmd(SnmpEngine(),
       CommunityData('public', mpModel=0),
       UdpTransportTarget(('demo.snmplabs.com', 161)),
       ContextData(),
       ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

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

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