您似乎正在使用netsnmp模块,而不是pysnmp模块.
如果要使用pysnmp,那么this example可能会有所帮助:
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.17.7.1.2.2.1.2'))):
if errorIndication or errorStatus:
print(errorIndication or errorStatus)
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
更新:
上面的循环每次迭代将获取一个OID值.如果要更有效地获取数据,一种选择是将更多OID填充到查询中(以许多ObjectType(…)参数的形式).
或者,您可以切换到GETBULK PDU类型,这可以通过将nextCmd调用更改为bulkCmd like this来完成.
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in bulkCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
0, 25, # fetch up to 25 OIDs one-shot
ObjectType(ObjectIdentity('1.3.6.1.2.1.17.7.1.2.2.1.2'))):
if errorIndication or errorStatus:
print(errorIndication or errorStatus)
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
请记住,GETBULK命令支持最初是在SNMP v2c中引入的,即您不能在SNMP v1上使用它.