Daniele
Daniele

Reputation: 21

PySnmp: unable to set value of OID ('str' object has no attribute 'getTagSet')

I'm using PySnmp library for Python3. I'm having issues trying to set a value using the complete OID. Everything is fine using MIB class/name instead. 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'. Could you help me with that?

Upvotes: 2

Views: 2348

Answers (2)

David
David

Reputation: 11

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.

The missing bit of the puzzle for me was finding the list of SNMP types. They are available at: 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:

#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!

Upvotes: 1

Ilya Etingof
Ilya Etingof

Reputation: 5555

This sounds like a bug, so I'd suggest opening an issue at Github along with a short reproducer. 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 i.e. OctetString("unlock").

Upvotes: 3

Related Questions