I need to controle a simple device via SNMP using Python 3.7 just to get it "ON" (1) and "OFF" (0). In the device manual, in the MIB information, there is a list of OID for each command (ex : GET output statue : 1.3.6........).
I manage to have the GET request working as I like (source : 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]))
However when I try to use SET the same way : (source : 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)
I get the following error :
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'))
I don't understand why it works without problem in one case and not in the other. In the device manual the instruction is the same as GET but there is STRING 0 or 1 at the end, I guess I'm missing something here but I can't found how to write it.
I just want to give this very simple instruction, if someone has an easy answer or alternative.
Thank you very much
PS : I also tried this tutorial ( https://www.ictshore.com/sdn/python-snmp-tutorial/ ) which make it's own functions, and again GET works but not SET. I got that my OID is not Object-TYPE.
When you pass a value to setCMD()
it (apparently) has to be a pysnmp.hlapi
object type. For example:
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)
It's possible that I'm missing a subtlety of how to use pysnmp.hlapi
, but this is the incantation that worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.