首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python的SNMP SET请求

使用Python的SNMP SET请求
EN

Stack Overflow用户
提问于 2019-02-11 22:39:05
回答 1查看 1.8K关注 0票数 1

我需要使用Python3.7通过SNMP控制一个简单的设备,只为了让它“开”(1)和“关”(0)。在设备手册的MIB信息中,有每个命令的OID列表(例如: GET output statue :1.3.6.)。

我设法让GET请求按我喜欢的方式工作(来源:http://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/snmp-versions.html):

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

我得到以下错误:

代码语言:javascript
复制
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相同,但末尾有字符串0或1,我猜我在这里遗漏了什么,但我找不到如何编写它。

我只是想给出这个非常简单的指导,如果有人有一个简单的答案或替代方案。

非常感谢

附言:我也尝试了这个教程(https://www.ictshore.com/sdn/python-snmp-tutorial/),它使它自己的函数,并再次获得工作,但没有设置。我知道我的OID不是对象类型。

EN

回答 1

Stack Overflow用户

发布于 2020-10-14 09:33:17

当您将一个值传递给setCMD()时,它(显然)必须是一个pysnmp.hlapi对象类型。例如:

代码语言:javascript
复制
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的一些微妙之处,但这就是对我有效的咒语。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54632957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档