OID(Object Identifier) 是一种用于唯一标识网络管理对象的数字序列。它类似于文件系统中的路径,用于在MIB(Management Information Base)中定位特定的网络设备参数。
pysnmp 是一个用于SNMP(Simple Network Management Protocol)操作的Python库。SNMP是一种广泛使用的网络管理协议,用于监控和管理网络设备。
类型:
应用场景:
以下是一个使用pysnmp读取OID并显示其变量或名称的示例代码:
from pysnmp.hlapi import *
def get_snmp_value(ip, community, oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community),
UdpTransportTarget((ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
# 示例调用
get_snmp_value('192.168.1.1', 'public', '1.3.6.1.2.1.1.1.0')
问题1:无法连接到设备
问题2:读取到的OID值为空
snmpwalk
工具验证OID是否存在。问题3:安全性问题(如使用SNMP v1/v2c)
通过以上信息,你应该能够理解如何使用pysnmp读取OID并显示其变量或名称,以及可能遇到的问题和解决方法。
领取专属 10元无门槛券
手把手带您无忧上云