每次使用python循环时,我可以保存分配给一个变量的所有值吗?

问题描述 投票:0回答:1

我有以下代码,可以完成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]
python loops variables pysnmp
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)
© www.soinside.com 2019 - 2024. All rights reserved.