最终,我希望能够以以下格式输出:
'接口lo启动'
输出为:
['IF-MIB::ifDescr.1=lo','IF-MIB::ifOperStatus.1=up']
['IF-MIB::ifDescr.2=eth0','IF-MIB::ifOperStatus.2=up']
代码:
from pysnmp.hlapi import *
for errorIndication,errorStatus,errorIndex,varBinds in nextCmd(SnmpEngine(), \
CommunityData('public', mpModel=0), \
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
lexicographicMode=False):
table = []
for varBind in varBinds:
table.append(varBind.prettyPrint().strip())
for i in table:
if i not in table:
table.append(i)
print(table)
for varBind in varBinds:
table.append(varBind.prettyPrint().strip())
for i in table:
if i not in table:
table.append(i)
print(table)
解析的PySNMP
ObjectType
由ObjectIdentity
和属于有效SNMP类型(在PySNMP中也称为objectSyntax
)的值组成。您可以使用Python的标准索引来访问这些元素。你知道吗在循环中,
varBinds
是一个由完全解析的ObjectType
组成的列表,对应于传递给nextCmd
的两个ObjectIdentity
。您可以解压varBinds
来反映每个objectType
,然后索引到每个objectSyntax
。当您调用它的prettyPrint
方法时,您将得到我们使用的可读字符串。你知道吗相关问题 更多 >
编程相关推荐