[英]PySnmp: unable to set value of OID ('str' object has no attribute 'getTagSet')
I'm using PySnmp library for Python3.我正在为 Python3 使用 PySnmp 库。 I'm having issues trying to set a value using the complete OID.我在尝试使用完整 OID 设置值时遇到问题。 Everything is fine using MIB class/name instead.使用 MIB 类/名称代替一切都很好。 What I'm trying to do basically is:我想做的基本上是:
objIdentity = ObjectIdentity('1.3.6.1.4.1.48112.1.1.1.0')
objType = ObjectType(objIdentity, "unlock")
errorIndication, errorStatus, errorIndex, varBinds = next(
setCmd(SnmpEngine(),
self.userData,
self.udpTarget,
ContextData(),
objType)
)
And what I obtain is AttributeError: 'str' object has no attribute 'getTagSet' .我得到的是AttributeError: 'str' object has no attribute 'getTagSet' 。 Could you help me with that?你能帮我吗?
This sounds like a bug, so I'd suggest opening an issue at Github along with a short reproducer.这听起来像是一个错误,所以我建议在 Github 上打开一个问题以及一个简短的复制器。 So we could follow it up from there.所以我们可以从那里跟进。
One suspicion that I have is that, without a MIB to look up value type, you have to give pysnmp SNMP-typed values ie OctetString("unlock")
.我的一个怀疑是,如果没有 MIB 来查找值类型,您必须为 pysnmp 提供 SNMP 类型的值,即OctetString("unlock")
。
Ilya's answer is right - I don't have enough rep to comment so I am adding another answer for future people that come here through google like I did. Ilya 的回答是正确的——我没有足够的代表发表评论,所以我正在为像我一样通过谷歌来到这里的未来人添加另一个答案。
The missing bit of the puzzle for me was finding the list of SNMP types.对我来说,缺少的一点是找到 SNMP 类型列表。 They are available at: https://pysnmp.readthedocs.io/en/latest/docs/api-reference.html#snmp-base-types它们位于: https://pysnmp.readthedocs.io/en/latest/docs/api-reference.html#snmp-base-types
To make my python work I changed the example from the documentation to:为了使我的 python 正常工作,我将文档中的示例更改为:
#fix true or false to int 1 or 0
if (type(new_value)==bool):
if new_value:
new_value = 1
else:
new_value = 0
#fix sent object as we are not using MIBs due to issues with some mibs breaking pysnmp.
#https://pysnmp.readthedocs.io/en/latest/docs/api-reference.html#snmp-base-types
fixed = False
if (type(new_value) == str):
pysnmp_object=ObjectType(ObjectIdentity(oid), OctetString(new_value))
fixed = True
if (type(new_value) == int):
pysnmp_object=ObjectType(ObjectIdentity(oid), Integer(new_value))
fixed = True
#raise an error if we havent been able to fix it.
if (fixed == False):
error = "The type of the new value must be a str or an int. It was of type: "+str(type(new_value))+"."
raise ValueError(error)
#example lifted from https://pysnmp.readthedocs.io/en/latest/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/modifying-variables.html
#also refer to https://pysnmp.readthedocs.io/en/latest/docs/hlapi/v3arch/asyncore/sync/manager/cmdgen/setcmd.html
snmp_iterator = (setCmd(
SnmpEngine(),
CommunityData(snmp_target.community_name),
UdpTransportTarget((snmp_target.ip_address, 161)),
ContextData(),
pysnmp_object,
lookupMib=False))
errorIndication, errorStatus, errorIndex, varBinds = next(snmp_iterator) # the iterator will only have one item in it and will return the results of the SNMP transmission.
I hope this helps someone else!我希望这可以帮助别人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.