Search code examples
pythonpython-3.xsnmppysnmp

How can I perform SNMP command for several OIDs at once?


I have a MIB table that I want to be able to edit. The following code performs the task well. What I would like to know is if there is a way to do it all in one set command

main_Status_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.1.0'

# Set the OIDs according to the Level argument
if Level == 1:
    Server_IP_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.2.1'
    Secret_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.6.1'
    Status_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.4.1'
elif Level == 2:
    Server_IP_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.2.2'
    Secret_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.6.2'
    Status_OID = '1.3.6.1.4.1.4515.1.3.23.1.1.2.1.4.2'
else:
    return -1

# Set the Main RADIUS Authentication Enable/Disable field
g = setCmd(SnmpEngine(), 
           CommunityData('admin', mpModel=1),
           UdpTransportTarget((Device_IP, 161)), 
           ContextData(),
           ObjectType(ObjectIdentity(main_Status_OID), Integer32(main_Status)))

next(g)

# Set the IP Address field of the Primary/Secondary RADIUS Server
g = setCmd(SnmpEngine(), 
           CommunityData('admin', mpModel=1),
           UdpTransportTarget((Device_IP, 161)), 
           ContextData(),
           ObjectType(ObjectIdentity(Server_IP_OID), IpAddress(Server_IP)))

next(g)

# Set the Shared Secret field of the Primary/Secondary RADIUS Server
g = setCmd(SnmpEngine(), 
           CommunityData('admin', mpModel=1),
           UdpTransportTarget((Device_IP, 161)), 
           ContextData(),
           ObjectType(ObjectIdentity(Secret_OID), OctetString(Secret)))

next(g)

# Set the Admin Status field of the Primary/Secondary RADIUS Server
g = setCmd(SnmpEngine(), 
           CommunityData('admin', mpModel=1),
           UdpTransportTarget((Device_IP, 161)), 
           ContextData(),
           ObjectType(ObjectIdentity(Status_OID), Integer32(Status)))

next(g)

I have tried to turn the ObjectType into a tuple of (ObjectIdentity(OID), Value) pairs but I got some errors. Is it possible to reduce all the "standalone" set commands into one set commands of 4 pairs ?

By the way, the if at the top of the code block is used to set the OID values so that I would be set to the right row in the table


Solution

  • From pysnmp prospective you surely can:

    ...
    g = setCmd(SnmpEngine(), 
        CommunityData('admin'),
        UdpTransportTarget((Device_IP, 161)), 
        ContextData(),
        ObjectType(ObjectIdentity(main_Status_OID), Integer32(main_Status))),
        ObjectType(ObjectIdentity(Server_IP_OID), IpAddress(Server_IP))),
        ObjectType(ObjectIdentity(Status_OID), Integer32(Status))
    )
    
    next(g)
    

    Hopefully, your SNMP agent supports that as well.