python snmp模块_python-如何使用pysnmp获取SNMP数据?

本文档介绍了如何使用Python的pysnmp库来获取SNMP数据。通过示例代码展示了使用CommunityData、UdpTransportTarget和ObjectType等组件进行SNMP查询的方法,包括单个OID查询和GETBULK请求以提高效率。
摘要由CSDN通过智能技术生成

您似乎正在使用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上使用它.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值