我正在尝试使用PySNMP遍历一个表,并且只拉回特定的字段。目前我有以下代码,它工作得很好。
def walkTable(community, hostname, port, tableName, fields):
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData(community, mpModel=0),
UdpTransportTarget((hostname, port)),
ContextData(),
ObjectType(ObjectIdentity(tableName, 'ifAlias')),
ObjectType(ObjectIdentity(tableName, 'ifHCInOctets')),
ObjectType(ObjectIdentity(tableName, 'ifHCOutOctets')),
lexicographicMode=False):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
break
else:
for varBind in varBinds:
fooBar(varBind)上面的函数接受一个名为field的变量。这是一个字符串数组,我想从表中拉出字段,但我不知道如何动态地执行此操作。
目前我尝试的是
def walkTable(community, hostname, port, tableName, fields):
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData(community, mpModel=0),
UdpTransportTarget((hostname, port)),
ContextData(),
for x in fields:
ObjectType(ObjectIdentity(tableName, x))
lexicographicMode=False):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
break
else:
for varBind in varBinds:
fooBar(varBind)这不管用。有没有办法为nextCmd()动态创建ObjectType?
发布于 2018-02-08 06:28:42
试着这样做:
...
nextCmd(SnmpEngine(),
CommunityData(community, mpModel=0),
UdpTransportTarget((hostname, port)),
ContextData(),
*[ObjectType(ObjectIdentity(tableName, field)) for field in fields])
...挑剔:如果您想要更高的性能,请保留持久的SnmpEngine实例,并将其重用于所有的SNMP调用。
https://stackoverflow.com/questions/48670140
复制相似问题