Jeff d'Ambly
Jeff d'Ambly

Reputation: 33

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. 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?

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

#!/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()))

Upvotes: 0

Views: 2125

Answers (1)

Ilya Etingof
Ilya Etingof

Reputation: 5555

If you are using pysnmp 4.2.*, pass your MIB object to the MibVariable class instance.

Or better use pysnmp 4.3 where ObjectType/ObjectIdentity classes are used for this purpose.

Example code (pysnmp 4.3):

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

Upvotes: 1

Related Questions