一次调用中的部分 SNMP table 检索?

Partial SNMP table retrieval in one call?

我正在寻找从 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```



您的代码看起来几乎正确!您只需将 nonRepeaters 值设置为 0 而不是 1。因为使用 1,您的第一个对象 (memSensorsTemperature) 没有得到迭代 - 它只是重新读取相同的 table 条目。

代码方面:

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 行的二维 table(取决于响应的内容)。

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.