'Not understanding why i am getting this error in python script

Hi I am using a python 3 script to do snmp set operation to one of switch.

The script is as follows.

import pysnmp

from pysnmp import hlapi

def cast(value):
    try:
        return int(value)
    except (ValueError, TypeError):
        try:
            return float(value)
        except (ValueError, TypeError):
            try:
                return str(value)
            except (ValueError, TypeError):
                pass
    return value

def fetch(handler, count):
    result = []
    for i in range(count):
        try:
            error_indication, error_status, error_index, var_binds = next(handler)
            if not error_indication and not error_status:
                items = {}
                for var_bind in var_binds:
                    items[str(var_bind[0])] = cast(var_bind[1])
                result.append(items)
            else:
                raise RuntimeError('Got SNMP error: {0}'.format(error_indication))
        except StopIteration:
            break
    return result


def construct_value_pairs(list_of_pairs):
    pairs = []
    for key, value in list_of_pairs.items():
        pairs.append(hlapi.ObjectType(hlapi.ObjectIdentity(key), value))
    return pairs

def set(target, value_pairs, credentials, port=161, engine=hlapi.SnmpEngine(), context=hlapi.ContextData()):
    handler = hlapi.setCmd(
        engine,
        credentials,
        hlapi.UdpTransportTarget((target, port)),
        context,
        *construct_value_pairs(value_pairs)
    )
    return fetch(handler, 1)[0]

If i run set('10.23.193.153', {'1.3.6.1.2.1.1.5.0': 'Test1'}, hlapi.CommunityData('public'))

The script is executed and the hostname on switch changes to Test1.

However if i do another set operation using

set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public'))

I am getting the following error. The above oid changes username of the switch.

> ============ RESTART: C:/Users/regop/Desktop/SNMP Password Reset.py ============ Traceback (most recent call last):   File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 53, in <module>
>     set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public'))   File
> "C:/Users/regop/Desktop/SNMP Password Reset.py", line 49, in set
>     return fetch(handler, 1)[0]   File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 22, in fetch
>     error_indication, error_status, error_index, var_binds = next(handler)   File
> "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\sync\cmdgen.py",
> line 214, in setCmd
>     cmdgen.setCmd(snmpEngine, authData, transportTarget,   File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\cmdgen.py",
> line 239, in setCmd
>     return cmdgen.SetCommandGenerator().sendVarBinds(   File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\entity\rfc3413\cmdgen.py",
> line 249, in sendVarBinds
>     v2c.apiPDU.setVarBinds(reqPDU, varBinds)   File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 131, in setVarBinds
>     apiVarBind.setOIDVal(   File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 42, in setOIDVal
>     varBind.setComponentByPosition(1).getComponentByPosition(1).setComponentByType(val.getTagSet(),
> val, verifyConstraints=False, matchTags=False, matchConstraints=False,
> innerFlag=True) AttributeError: 'str' object has no attribute
> 'getTagSet'

Not sure what different I am doing here.



Solution 1:[1]

This unhandled exception feels like a bug in pysnmp, but that's irrelevant to your question.

My guess is that the problem is caused by your second OID not being resolved at a MIB and therefore the value (admin) not automatically casted into some SNMP type object.

The solution is one of:

  • Refer to your SNMP objects by their symbolic names (MIB, symbol, instance ID)
  • Keep using OIDs, but pre-load the MIB where that OID is defined
  • Keep using OIDs, but pass your values as SNMP objects so that no MIBs would be necessary

Solution 2:[2]

I have the same error, Because of my SNMP Device doesn't support 'integer' to write, just 'String'. I suggest to check your OID address whether it supports 'String' to write or not.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ilya Etingof
Solution 2 Sepehr roosta