问题描述
我正在尝试使用 SNMP get 设置一个值。我已经测试过 OID 可以用 MIB 浏览器写入。我可以使用另一个脚本获取值,但使用集合没有意义。
from pysnmp.hlapi import *
engine = SnmpEngine()
community = CommunityData('public',mpModel=1)
transport = UdpTransportTarget(('target',161))
context = ContextData()
# Your OID goes here.
identity = ObjectIdentity('.1.3.6.1.4.1.32050')
# 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)
我看到的错误
line 17,in <module>
errorIndication,varBinds = next(g)
pysnmp.smi.error.SmiError: ObjectIdentity object not properly initialized
解决方法
在 context
之后,setCmd
期望类 ObjectType
的参数(即一对 oid + value),这里不是这种情况。在我看来,identity
在这里是多余的。
试试这个:
g = setCmd(engine,community,transport,context,type,lookupMib=False)
(顺便说一句,您应该避免调用变量 type
,这是一个预定义的 Python 函数。)
在您修复上一个答案中描述的问题后,仅通过代码检查,它就会提示 OID .1.3.6.1.4.1.32050 不能是对象实例,因为它是企业分支。您可以通过将 identity 设置为 .1.3.6.1.2.1.1.5.0 来在不到一分钟的时间内证明这一点。