0

I have the following code

def SetSnmpOid(oid, snmpDataType, value):
    try:
        global SNMP_COMMUNITY
        global SNMP_HOST
        global SNMP_PORT

        cmdGen = cmdgen.CommandGenerator()

        # Send to first Telnet IP address, different port for SNMP
        dataType = "rfc1902." + snmpDataType
        errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd(
                cmdgen.CommunityData(SNMP_COMMUNITY, mpModel=0),
                cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)), 
                                           (oid + "." + str(0), dataType(value)))

which, when run, generates this error

File "H:/code/test_script.txt.py", line 105, in SetSnmpOid
    (oid + "." + str(0), dataType(value)))
TypeError: 'str' object is not callable

and I'm certain that the error refers to dataType (which evaluates to rfc1902.integer, which is correct). The problem seems to be using it as a function call dataType(value) as part of a tuple.

If anyone is interested, I am trying to emulate the code found here.

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

1

This looks like an issue with trying dynamically build a reference to a function.

You're wanting to create a rfc1902.integer object which gets passed as an argument to the cmdgen.UdpTransportTarget. The problem is, the value of dataType is a string, not a callable. Once all the code is evaluated, you end up with this:

'rfc1902.integer'(value)

when what you really want is this:

rfc1902.integer(value)

To get the callable, you have two options

  1. load it using getattr
  2. load it using a dict "switch statement."
Community
  • 1
  • 1
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30