0

我有以下代码可以正常运行 snmp,但是当我尝试使用接口号循环“t”提取设备(wlc)上的所有序列号时,它与打印功能很好,但是当它将它保存到变量中时vaBinds[3][1] 它只保存最后一个。每次循环时如何保存所有变量值。

不确定我的问题是否有意义,但请尽可能提供帮助。先感谢您。

        for t in range(1, 3):
            t = str(t)
            t.strip()

            errorIndication, errorStatus, errorIndex, varBinds = next(
                getCmd(SnmpEngine(),
                       CommunityData(item, mpModel=0 or 1),
                       UdpTransportTarget((str(i), 161), timeout=0, retries=0),
                       ContextData(),
                       ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)),  # 0
                       ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)),  # 1
                       ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)),  # 2
                       ObjectType(ObjectIdentity('ENTITY-MIB', 'entPhysicalSerialNum', t)),  # 3
                       ObjectType(ObjectIdentity('ENTITY-MIB', 'entPhysicalModelName', t))),  # 4

            )
            # print(varBinds[3][1])

            if errorIndication:
                print(errorIndication)
            elif errorStatus:
                print('%s at %s' % (errorStatus.prettyPrint(i),
                                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))  # error exceptions

            else:
                for varBind in varBinds:
                    B = varBinds[0]
4

1 回答 1

0

如果我有您的问题,您正在引用一个位于可变容器数据结构中某处的值。由于是可变的,它在每次调用时都会发生变化。

如果是这种情况,也许您可​​以在每次迭代时引用标量(不可变)值:

 my_values = []

 for t in range(1, 3):
     # perform SNMP GET
     ...
     my_values.extend((vb[0], vb[1]) for vb in var_binds)
于 2020-01-26T16:01:54.877 回答