2

trying to convert this line in to pysnmp

snmpset -v 2c -On -r 5 -t 2 -c private ip-address .1.3.6.1.2.1.2.2.1.7.369098771 i 1

I am trying to take a working walk function and modify it but my knowledge with SNMP makes it very hard to understand pysnmp doc

this is just part of the code

from pysnmp.entity.rfc3413.oneliner import cmdgen

        device_target = (self.ip, self.port)
        res = None
        # Create a PYSNMP cmdgen object
        cmd_gen = cmdgen.CommandGenerator()
        (error_detected, error_status, error_index, snmp_data) = cmd_gen.setCmd(
            cmdgen.CommunityData(community_string),
            cmdgen.UdpTransportTarget(device_target), '.1.3.6.1.2.1.2.2.1.7.369098771', 1
            lookupNames=True, lookupValues=True)

I know I am missing something, can any one help please

1 Answers1

1

I'd highly recommend upgrading your pysnmp to the latest released version and use "hlapi" interface.

from pysnmp.hlapi import *

device_target = (self.ip, self.port)
community_string = 'private'

cmd_gen = setCmd(SnmpEngine(),
                 CommunityData(community_string),
                 UdpTransportTarget(device_target, timeout=2.0, retries=5),
                 ContextData(),
                 ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.7.369098771'), Integer32(1)),
                 lookupMib=False
)

res = None  # True denotes a failure

errorIndication, errorStatus, errorIndex, varBinds = next(cmd_gen)

if errorIndication:
    res = errorIndication
elif errorStatus:
    res = '%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?')

if res:
    print('SNMP failed: %s' % res)

References: here

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21