python - 使用 Python 的 SNMP SET 请求
问题描述
我需要使用 Python 3.7 通过 SNMP 控制一个简单的设备,以使其“打开”(1)和“关闭”(0)。在设备手册中,在 MIB 信息中,每个命令都有一个 OID 列表(例如:GET 输出状态:1.3.6........)。
我设法让 GET 请求按我喜欢的方式工作(来源:http ://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/snmp-versions.html ):
from pysnmp.hlapi import *
g = getCmd(SnmpEngine()
, CommunityData('public', mpModel=1)
, hlapi.UdpTransportTarget(('DEVICE IP', 161))
, ContextData()
, ObjectType(ObjectIdentity('GET OID given by the device manual')))
errorIndication, errorStatus, errorIndex, varBinds = next(g)
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
但是,当我尝试以相同的方式使用 SET 时:(来源:http ://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/modifying-variables.html )
from pysnmp.hlapi import *
g = setCmd(SnmpEngine()
, CommunityData('public', mpModel=1)
, hlapi.UdpTransportTarget(('DEVICE IP', 161))
, ContextData()
, ObjectType(ObjectIdentity('SET OID given by the device manual, which is the same as the GET'), '1') #1 = new value
)
errorIndication, errorStatus, errorIndex, varBinds = next(g)
print(errorIndication, varBinds)
我收到以下错误:
MibNotFoundError: SET OID compilation error(s): missingcaused by <class 'pysnmp.smi.error.MibNotFoundError'>: MIB file "SET OID.py[co]" not found in search path (DirMibSource('/home/username/anaconda3/lib/python3.7/site-packages/pysnmp/smi/mibs'), DirMibSource('/home/username/anaconda3/lib/python3.7/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/username/.pysnmp/mibs'))
我不明白为什么它在一种情况下没有问题而在另一种情况下没有问题。在设备手册中,指令与 GET 相同,但末尾有 STRING 0 或 1,我想我在这里遗漏了一些东西,但我找不到如何编写它。
如果有人有简单的答案或替代方案,我只想给出这个非常简单的指令。
非常感谢
PS:我还尝试了本教程(https://www.ictshore.com/sdn/python-snmp-tutorial/),它使其成为自己的功能,并且再次 GET 工作但不是 SET。我知道我的 OID 不是 Object-TYPE。
解决方案
当您将值传递给setCMD()
它时(显然)必须是pysnmp.hlapi
对象类型。例如:
from pysnmp.hlapi import *
engine = SnmpEngine()
community = CommunityData('public', mpModel=1)
transport = UdpTransportTarget(('192.168.1.1', 161))
context = ContextData()
# Your OID goes here.
identity = ObjectIdentity('1.3.6.1.4.1.534.6.6.7.6.1.1.3.0')
# If this was a string value, use OctetString() instead of Integer().
new_value = Integer(1)
type = ObjectType(identity, new_value)
# Setting lookupMib=False here because this example uses a numeric OID.
g = setCmd(engine, community, transport, context, identity, type, lookupMib=False)
errorIndication, errorStatus, errorIndex, varBinds = next(g)
print(errorIndication, varBinds)
我可能错过了如何使用的微妙之处pysnmp.hlapi
,但这是对我有用的咒语。
推荐阅读
- windows - 从 dotnetcore 应用程序将指标推送到 statsd
- r - R 编程 - 错误
- observable - Angular 6 Observable 链接和错误处理
- dart - 是否可以在颤振中使用 sqlite 而不是 sqflite?如果是,那么如何?
- go - 带秘密的SHA1编码,相当于PHP hash_hmac
- c++ - 学习嵌套循环 C++ 与示例混淆
- javascript - JavaScript 如何更改函数内部的参数?
- c# - 如何确定反序列化的 json 字符串是什么类型?
- .net - 使定时器间隔小于1毫秒.Net
- python - 如何在右上角显示时间和日期的同时让温度显示在左上角?