在一次调用中检索部分SNMP表?

2024-10-02 14:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我要从Power NET MIB中检索memSensorsTemperaturememSensorsHumidity的值。我不确定我是否需要打两次电话,或者是否有一个单一的电话只是为了剔除我需要的特定OID的方法。当我在一个调用中同时包含这两个调用时,它看起来像是在做某种嵌套的操作

每个OID调用中有6个值。。。所以6个温度和6个嗡嗡声读数

#!/usr/bin/env python

from pysnmp.hlapi import *

result1 = bulkCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('xx.xx.xx.xx', 161)),
           ContextData(),
           1, 6,
           ObjectType(
            ObjectIdentity('PowerNet-MIB', 'memSensorsTemperature').addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
            ),
           ObjectType(
            ObjectIdentity('PowerNet-MIB', 'memSensorsHumidity').addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
            ),
               lexicographicMode=False
)
#errorIndication, errorStatus, errorIndex, varBinds = next()
#memSensorsTemperature
for errorIndication, errorStatus, errorIndex, varBinds in result1:
  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]))

结果(截断)

PowerNet-MIB::memSensorsHumidity.0.1 = 16
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.2 = 56
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.3 = 16
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.4 = 41
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.5 = 46
PowerNet-MIB::memSensorsTemperature.0.1 = 81
PowerNet-MIB::memSensorsHumidity.0.6 = -1
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View
PowerNet-MIB::memSensorsTemperature.0.2 = 80
PowerNet-MIB::memSensorsHumidity.0.6 = No more variables left in this MIB View```




Tags: noinviewmorevariablesthisleftmib
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:32

你的代码似乎几乎正确!您只需要将nonRepeaters值设置为0,而不是1。因为使用1,您的第一个对象(memSensorsTemperature)不会被迭代-它只是重新读取相同的表条目

代码方面:

result1 = bulkCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('xx.xx.xx.xx', 161)),
           ContextData(),
           0, 6,  # get up to 6 follow-up OIDs per each requested OID
           ObjectType(ObjectIdentity(
               'PowerNet-MIB', 'memSensorsTemperature').addAsn1MibSource(
                   'http://mibs.snmplabs.com/asn1/@mib@')
           ),
           ObjectType(ObjectIdentity('PowerNet-MIB', 'memSensorsHumidity')),
           lexicographicMode=False
)

响应将始终是一个包含0到6行的二维表(取决于响应中的内容)

RFC1905解释了nonRepeaters的语义:

   The values of the non-repeaters and max-repetitions fields in the
   request specify the processing requested.  One variable binding in
   the Response-PDU is requested for the first N variable bindings in
   the request and M variable bindings are requested for each of the R
   remaining variable bindings in the request.  Consequently, the total
   number of requested variable bindings communicated by the request is
   given by N + (M * R), where N is the minimum of:  a) the value of the
   non-repeaters field in the request, and b) the number of variable
   bindings in the request; M is the value of the max-repetitions field
   in the request; and R is the maximum of:  a) number of variable
   bindings in the request - N, and b)  zero.

相关问题 更多 >

    热门问题