简体   繁体   中英

Querying numerical OID in PySNMP?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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