我有以下代码,可以完成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]
如果我有您的问题,则您所引用的值位于可变容器数据结构中的某个位置。由于可变,它会在每次调用时发生变异。
如果是这种情况,也许您可以在每次迭代中引用标量(不可变)值:
my_values = []
for t in range(1, 3):
# perform SNMP GET
...
my_values.extend((vb[0], vb[1]) for vb in var_binds)