簡體   English   中英

Python PySNMP:無法獲取OID

[英]Python PySNMP: unable to fetch OID

我似乎無法使用pysnmp獲取SNMP。
python2和3的結果相同。
設備使用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

雖然snmpwalk可以正常工作:

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

這是我的代碼:

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

您正在執行GET iso.3.6.1.4.1.5597.30.0.2.2 ,而snmpwalk報告僅存在OID iso.3.6.1.4.1.5597.30.0.2.2.0

嘗試使用此代碼(如本例所示 )。 它使用了更新且更簡潔的pysnmp API,盡管一旦修復了要查詢的OID,您的代碼也將正常工作。

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]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM