2015-11-23 54 views
0

我想弄清楚如何加載mib並獲取思科交換機的sysObjectID,然後將其解析爲人類可讀的內容。現在我的代碼可以工作,但是讓我感到困惑的是getCmd()的參數,而不是使用OID發送字符串,我如何發送託管對象?pysnmp snmp獲取託管對象

我怎麼能更改以下線在我的代碼

'1.3.6.1.2.1.1.2.0', 

要像「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())) 

回答

1

如果您正在使用pysnmp 4.2。* ,將您的MIB對象傳遞給MibVariable類實例。

或更好地使用pysnmp 4.3,其中ObjectType/ObjectIdentity類用於此目的。

實施例的代碼(pysnmp 4.3):

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