简体   繁体   中英

Python PySNMP: unable to fetch OID

I seem to be unable to fetch SNMP using pysnmp.
Same result with python2 and 3.
The device uses SNMP v2.

SNMPv2-SMI::enterprises.5597.30.0.2.2 = No Such Object currently exists 
at this OID
SNMPv2-SMI::enterprises.5597.30.0.2.4 = No Such Object currently exists 
at this OID

While snmpwalk works fine:

snmpwalk -v1 -cpublic 10.0.1.8 1.3.6.1.4.1.5597.30.0.2.2
iso.3.6.1.4.1.5597.30.0.2.2.0 = INTEGER: 1

This is my code:

from pysnmp.entity.rfc3413.oneliner import cmdgen
import time

SNMP_HOST = '10.0.1.8'
SNMP_PORT = 161
SNMP_COMMUNITY = 'public'

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
'1.3.6.1.4.1.5597.30.0.2.2',
'1.3.6.1.4.1.5597.30.0.2.4'
)

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

You are doing GET iso.3.6.1.4.1.5597.30.0.2.2 , while snmpwalk reports that only OID iso.3.6.1.4.1.5597.30.0.2.2.0 exists.

Try this code (as taken from this example ). It uses the newer and more concise pysnmp API, though your code should work as well once you fix the OID you query for.

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('10.0.1.8', 161)),
           ContextData(),
           ObjectType(ObjectIdentity('1.3.6.1.4.1.5597.30.0.2.2.0')))
)

if errorIndication:
    print(errorIndication)
elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

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