[英]SNMP SET request using Python
我需要使用 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
,但這是對我pysnmp.hlapi
的咒語。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.