Querying numerical OID in PySNMP?

victorhooi picture victorhooi · Apr 3, 2013 · Viewed 7.9k times · Source

I'm attempting to query an SNMP variable on a Cisco routing device in Python, and struggling.

I have a snmpwalk command that works fine:

$snmpwalk -v2c -c <our_community_string> <device_ip_address> 1.3.6.1.4.1.9.9.42.1.2.10.1.1.950
SNMPv2-SMI::enterprises.9.9.42.1.2.10.1.1.950 = Gauge32: 68

Now I'm trying to do the same thing in Python using pysnmp.

I tried using something based on the examples here - http://pysnmp.sourceforge.net/examples/current/index.html - but got an SmiError:

In [1]: from pysnmp.entity.rfc3413.oneliner import cmdgen

In [2]: cmdGen = cmdgen.CommandGenerator()

In [3]: errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
...:     cmdgen.CommunityData('0pe3aro'),
...:     cmdgen.UdpTransportTarget(('10.65.113.28', 161)),
...:     cmdgen.MibVariable('1.3.6.1.4.1.9.9.42.1.2.10.1.1.950', 0)
...: )

But I get the following:

SmiError: MIB file "1.3.6.1.4.1.9.9.42.1.2.10.1.1.950.py[co]" not found in search path

Basically - I wanted the equavilent of this in NetSNMP, but in PySNMP (http://ben.akrin.com/?p=1234).

Does anybody know a simple way to query a numerical OID in PySNMP?

Cheers, Victor

Answer

Pooh picture Pooh · Apr 26, 2013

I believe the following code would work for you:

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
    '1.3.6.1.2.1.1.3.0'
)

print('\n'.join([ '%s = %s' % varBind for varBind in varBinds]))

You could cut&paste it into your Python prompt to try and experiment with it.

The MibVariable object can be used for referring to a MIB symbol by name.