cmdgen.py

class CommandGenerator(object):
    _null = univ.Null('')

    def __init__(self, snmpEngine=None, asynCmdGen=None):
        # compatibility attributes
        self.snmpEngine = snmpEngine or SnmpEngine()

    def getCmd(self, authData, transportTarget, *varNames, **kwargs):
        if 'lookupNames' not in kwargs:
            kwargs['lookupNames'] = False
        if 'lookupValues' not in kwargs:
            kwargs['lookupValues'] = False
        errorIndication, errorStatus, errorIndex, varBinds = None, 0, 0, []
        for (errorIndication,
             errorStatus,
             errorIndex,
             varBinds) in sync.getCmd(self.snmpEngine, authData, transportTarget,
                                      ContextData(kwargs.get('contextEngineId'),
                                                  kwargs.get('contextName', null)),
                                      *[(x, self._null) for x in varNames],
                                      **kwargs):
            break
        return errorIndication, errorStatus, errorIndex, varBinds

    def setCmd(self, authData, transportTarget, *varBinds, **kwargs):
        if 'lookupNames' not in kwargs:
            kwargs['lookupNames'] = False
        if 'lookupValues' not in kwargs:
            kwargs['lookupValues'] = False
        errorIndication, errorStatus, errorIndex, rspVarBinds = None, 0, 0, []
        for (errorIndication,
             errorStatus,
             errorIndex,
             rspVarBinds) in sync.setCmd(self.snmpEngine, authData, transportTarget,
                                         ContextData(kwargs.get('contextEngineId'),
                                                     kwargs.get('contextName', null)),
                                         *varBinds,
                                         **kwargs):
            break

        return errorIndication, errorStatus, errorIndex, rspVarBinds

    def nextCmd(self, authData, transportTarget, *varNames, **kwargs):
        if 'lookupNames' not in kwargs:
            kwargs['lookupNames'] = False
        if 'lookupValues' not in kwargs:
            kwargs['lookupValues'] = False
        if 'lexicographicMode' not in kwargs:
            kwargs['lexicographicMode'] = False
        errorIndication, errorStatus, errorIndex = None, 0, 0
        varBindTable = []
        for (errorIndication,
             errorStatus,
             errorIndex,
             varBinds) in sync.nextCmd(self.snmpEngine, authData, transportTarget,
                                       ContextData(kwargs.get('contextEngineId'),
                                                   kwargs.get('contextName', null)),
                                       *[(x, self._null) for x in varNames],
                                       **kwargs):
            if errorIndication or errorStatus:
                return errorIndication, errorStatus, errorIndex, varBinds

            varBindTable.append(varBinds)

        return errorIndication, errorStatus, errorIndex, varBindTable

    def bulkCmd(self, authData, transportTarget,
                nonRepeaters, maxRepetitions, *varNames, **kwargs):
        if 'lookupNames' not in kwargs:
            kwargs['lookupNames'] = False
        if 'lookupValues' not in kwargs:
            kwargs['lookupValues'] = False
        if 'lexicographicMode' not in kwargs:
            kwargs['lexicographicMode'] = False
        errorIndication, errorStatus, errorIndex = None, 0, 0
        varBindTable = []
        for (errorIndication,
             errorStatus,
             errorIndex,
             varBinds) in sync.bulkCmd(self.snmpEngine, authData,
                                       transportTarget,
                                       ContextData(kwargs.get('contextEngineId'),
                                                   kwargs.get('contextName', null)),
                                       nonRepeaters, maxRepetitions,
                                       *[(x, self._null) for x in varNames],
                                       **kwargs):
            if errorIndication or errorStatus:
                return errorIndication, errorStatus, errorIndex, varBinds

            varBindTable.append(varBinds)

        return errorIndication, errorStatus, errorIndex, varBindTable
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页