Пример #1
0
def snmpOperation(snmpEngine, target, contextEngineId, contextName, varBinds):
    ( snmpEngine,
      errorIndication,
      errorStatus,
      errorIndex, 
      varBinds ) = yield from cmdgen.SetCommandGenerator().sendVarBinds(
            snmpEngine,
            target,
            contextEngineId,
            contextName,
            varBinds
      )

    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex)-1][0] or '?'
            )
        )
    else:
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

    # This also terminates internal timer
    config.delTransport(
        snmpEngine,
        udp.domainName
    ).closeTransport()
Пример #2
0
def snmpOperation(snmpEngine, target, snmpContext, contextName,
                  notificationName, instanceIndex, additionalVarBinds):
    ( snmpEngine,
      errorIndication,
      errorStatus,
      errorIndex, 
      varBinds ) = yield from ntforg.NotificationOriginator().sendVarBinds(
        snmpEngine,
        target,
        snmpContext,
        contextName,
        notificationName,
        instanceIndex,
        additionalVarBinds
    )

    print('Notification status - %s' % (
            errorIndication and errorIndication or 'delivered'
        )
    )

    # This also terminates internal timer
    config.delTransport(
        snmpEngine,
        udp.domainName
    ).closeTransport()
Пример #3
0
def snmpOperation(snmpEngine, target, snmpContext, contextName,
                  notificationName, instanceIndex, additionalVarBinds):
    future = ntforg.NotificationOriginator().sendVarBinds(
        snmpEngine,
        target,
        snmpContext,
        contextName,
        notificationName,
        instanceIndex,
        additionalVarBinds
    )

    # We know we are sending TRAP which will never produce any response.
    # Therefore we simulate successful response here.
    future.set_result((snmpEngine, None, 0, 0, ()))

    ( snmpEngine,
      errorIndication,
      errorStatus,
      errorIndex, 
      varBinds ) = yield trollius.From(future)
 
    if errorIndication:
        print(errorIndication)

    # This also terminates internal timer
    config.delTransport(
        snmpEngine,
        udp.domainName
    ).closeTransport()
Пример #4
0
def snmpOperation(snmpEngine, target, contextEngineId, contextName,
                  nonRepeaters, maxRepetitions, varBinds):
    initialVarBinds = varBinds
    while varBinds:
        ( snmpEngine,
          errorIndication,
          errorStatus,
          errorIndex, 
          varBindTable ) = yield trollius.From(
              cmdgen.BulkCommandGenerator().sendVarBinds(
                snmpEngine,
                target,
                contextEngineId,
                contextName,
                nonRepeaters,
                maxRepetitions,
                varBinds
              )
          )

        if errorIndication:
            print(errorIndication)
            break
        elif errorStatus:
            print('%s at %s' % (
                    errorStatus.prettyPrint(),
                    errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?'
                )
            )
            break
        else:
            for varBindRow in varBindTable:
                for oid, val in varBindRow:
                    print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

            errorIndication, varBinds = cmdgen.getNextVarBinds(
                initialVarBinds, varBindRow
            )
             
    # This also terminates internal timer
    config.delTransport(
        snmpEngine,
        udp.domainName
    ).closeTransport()

    loop.stop()
Пример #5
0
def snmpOperation(snmpEngine, target, snmpContext, contextName,
                  notificationName, instanceIndex, additionalVarBinds):
    future = ntforg.NotificationOriginator().sendVarBinds(
        snmpEngine, target, snmpContext, contextName, notificationName,
        instanceIndex, additionalVarBinds)

    # We know we are sending TRAP which will never produce any response.
    # Therefore we simulate successful response here.
    future.set_result((snmpEngine, None, 0, 0, ()))

    (snmpEngine, errorIndication, errorStatus, errorIndex,
     varBinds) = yield trollius.From(future)

    if errorIndication:
        print(errorIndication)

    # This also terminates internal timer
    config.delTransport(snmpEngine, udp.domainName)
Пример #6
0
    def unconfigure(self, snmpEngine, authData=None):
        cache = self._getCache(snmpEngine)
        if authData:
            if isinstance(authData, CommunityData):
                authDataKey = authData.communityIndex
            elif isinstance(authData, UsmUserData):
                authDataKey = authData.userName, authData.securityEngineId
            else:
                raise error.PySnmpError('Unsupported authentication object')
            if authDataKey in cache['auth']:
                authDataKeys = (authDataKey,)
            else:
                raise error.PySnmpError('Unknown authData %s' % (authData,))
        else:
            authDataKeys = list(cache['auth'].keys())

        addrNames, paramsNames = set(), set()

        for authDataKey in authDataKeys:
            authDataX = cache['auth'][authDataKey]
            del cache['auth'][authDataKey]
            if isinstance(authDataX, CommunityData):
                config.delV1System(
                    snmpEngine,
                    authDataX.communityIndex
                )
            elif isinstance(authDataX, UsmUserData):
                config.delV3User(
                    snmpEngine,
                    authDataX.userName,
                    authDataX.securityEngineId
                )
            else:
                raise error.PySnmpError('Unsupported authentication object')

            paramsKey = (authDataX.securityName,
                         authDataX.securityLevel,
                         authDataX.mpModel)
            if paramsKey in cache['parm']:
                paramsName, useCount = cache['parm'][paramsKey]
                useCount -= 1
                if useCount:
                    cache['parm'][paramsKey] = paramsName, useCount
                else:
                    del cache['parm'][paramsKey]
                    config.delTargetParams(
                        snmpEngine, paramsName
                    )
                    paramsNames.add(paramsName)
            else:
                raise error.PySnmpError('Unknown target %s' % (paramsKey,))

            addrKeys = [x for x in cache['addr'] if x[0] == paramsName]

            for addrKey in addrKeys:
                addrName, useCount = cache['addr'][addrKey]
                useCount -= 1
                if useCount:
                    cache['addr'][addrKey] = addrName, useCount
                else:
                    config.delTargetAddr(snmpEngine, addrName)

                    addrNames.add(addrKey)

                    if addrKey[1] in cache['tran']:
                        transport, useCount = cache['tran'][addrKey[1]]
                        if useCount > 1:
                            useCount -= 1
                            cache['tran'][addrKey[1]] = transport, useCount
                        else:
                            config.delTransport(snmpEngine, addrKey[1])
                            transport.closeTransport()
                            del cache['tran'][addrKey[1]]

        return addrNames, paramsNames
Пример #7
0
# Error/response receiver
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus,
          errorIndex, varBinds, cbCtx):
    if errorIndication:
        print(errorIndication)

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

    else:
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))


# Prepare and send a request message
cmdgen.GetCommandGenerator().sendVarBinds(
    snmpEngine,
    'my-router',
    None,
    '',  # contextEngineId, contextName
    [((1, 3, 6, 1, 2, 1, 1, 1, 0), None)],
    cbFun)

# Run I/O dispatcher which would send pending queries and process responses
snmpEngine.transportDispatcher.runDispatcher()

config.delTransport(snmpEngine, udp.DOMAIN_NAME).closeTransport()
Пример #8
0
def cbFun(snmpEngine, sendRequestHandle, errorIndication,
          errorStatus, errorIndex, varBinds, cbCtx):
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1][0] or '?'
            )
        )
    else:
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

# Prepare and send a request message
cmdgen.GetCommandGenerator().sendVarBinds(
    snmpEngine,
    'my-router',
    None, '',  # contextEngineId, contextName
    [ ((1,3,6,1,2,1,1,1,0), None) ],
    cbFun
)

# Run I/O dispatcher which would send pending queries and process responses
snmpEngine.transportDispatcher.runDispatcher()

config.delTransport(
    snmpEngine,
    udp.domainName
).closeTransport()
Пример #9
0
    def uncfgCmdGen(self, authData=None):
        if authData:
            if isinstance(authData, CommunityData):
                authDataKey = authData.communityIndex
            elif isinstance(authData, UsmUserData):
                authDataKey = authData.userName, authData.securityEngineId
            else:
                raise error.PySnmpError('Unsupported authentication object')
            if authDataKey in self.__knownAuths:
                authDataKeys = (authDataKey, )
            else:
                raise error.PySnmpError('Unknown authData %s' % (authData, ))
        else:
            authDataKeys = list(self.__knownAuths.keys())

        addrNames, paramsNames = set(), set()

        for authDataKey in authDataKeys:
            authDataX = self.__knownAuths[authDataKey]
            del self.__knownAuths[authDataKey]
            if isinstance(authDataX, CommunityData):
                config.delV1System(self.snmpEngine, authDataX.communityIndex)
            elif isinstance(authDataX, UsmUserData):
                config.delV3User(self.snmpEngine, authDataX.userName,
                                 authDataX.securityEngineId)
            else:
                raise error.PySnmpError('Unsupported authentication object')

            paramsKey = authDataX.securityName, \
                        authDataX.securityLevel, \
                        authDataX.mpModel
            if paramsKey in self.__knownParams:
                paramsName, useCount = self.__knownParams[paramsKey]
                useCount -= 1
                if useCount:
                    self.__knownParams[paramsKey] = paramsName, useCount
                else:
                    del self.__knownParams[paramsKey]
                    config.delTargetParams(self.snmpEngine, paramsName)
                    paramsNames.add(paramsName)
            else:
                raise error.PySnmpError('Unknown target %s' % (paramsKey, ))

            addrKeys = [
                x for x in self.__knownTransportAddrs if x[0] == paramsName
            ]

            for addrKey in addrKeys:
                addrName, useCount = self.__knownTransportAddrs[addrKey]
                useCount -= 1
                if useCount:
                    self.__knownTransportAddrs[addrKey] = addrName, useCount
                else:
                    config.delTargetAddr(self.snmpEngine, addrName)

                    addrNames.add(addrKey)

                    if addrKey[1] in self.__knownTransports:
                        transport, useCount = self.__knownTransports[
                            addrKey[1]]
                        if useCount > 1:
                            useCount -= 1
                            self.__knownTransports[
                                addrKey[1]] = transport, useCount
                        else:
                            config.delTransport(self.snmpEngine, addrKey[1])
                            transport.closeTransport()
                            del self.__knownTransports[addrKey[1]]

        return addrNames, paramsNames
Пример #10
0
    def unconfigure(self,
                    snmpEngine,
                    authData=None,
                    contextName=null,
                    **options):
        cache = self._getCache(snmpEngine)

        if authData:
            if isinstance(authData, CommunityData):
                authDataKey = authData.communityIndex

            elif isinstance(authData, UsmUserData):
                authDataKey = authData.userName, authData.securityEngineId

            else:
                raise error.PySnmpError('Unsupported authentication object')

            if authDataKey in cache['auth']:
                authDataKeys = (authDataKey, )

            else:
                raise error.PySnmpError('Unknown authData %s' % (authData, ))

        else:
            authDataKeys = list(cache['auth'].keys())

        addrNames, paramsNames = set(), set()

        for authDataKey in authDataKeys:
            authDataX = cache['auth'][authDataKey]

            del cache['auth'][authDataKey]

            if isinstance(authDataX, CommunityData):
                config.delV1System(snmpEngine, authDataX.communityIndex)

            elif isinstance(authDataX, UsmUserData):
                config.delV3User(snmpEngine, authDataX.userName,
                                 authDataX.securityEngineId)

            else:
                raise error.PySnmpError('Unsupported authentication object')

            paramsKey = (authDataX.securityName, authDataX.securityLevel,
                         authDataX.mpModel)

            if paramsKey in cache['parm']:
                paramsName, useCount = cache['parm'][paramsKey]

                useCount -= 1

                if useCount:
                    cache['parm'][paramsKey] = paramsName, useCount

                else:
                    del cache['parm'][paramsKey]

                    config.delTargetParams(snmpEngine, paramsName)

                    paramsNames.add(paramsName)

            else:
                raise error.PySnmpError('Unknown target %s' % (paramsKey, ))

            addrKeys = [x for x in cache['addr'] if x[0] == paramsName]

            for addrKey in addrKeys:
                addrName, useCount = cache['addr'][addrKey]

                useCount -= 1

                if useCount:
                    cache['addr'][addrKey] = addrName, useCount

                else:
                    config.delTargetAddr(snmpEngine, addrName)

                    del cache['addr'][addrKey]

                    addrNames.add(addrKey)

                    if addrKey[1] in cache['tran']:
                        transport, useCount = cache['tran'][addrKey[1]]

                        if useCount > 1:
                            useCount -= 1
                            cache['tran'][addrKey[1]] = transport, useCount

                        else:
                            config.delTransport(snmpEngine, addrKey[1])
                            transport.closeTransport()

                            del cache['tran'][addrKey[1]]

        return addrNames, paramsNames
Пример #11
0
def cbFun(snmpEngine, sendRequestHandle, errorIndication,
          errorStatus, errorIndex, varBinds, cbCtx):
    if errorIndication:
        print(errorIndication)

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

    else:
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))


# Prepare and send a request message
cmdgen.GetCommandGenerator().sendVarBinds(
    snmpEngine,
    'my-router',
    None, '',  # contextEngineId, contextName
    [((1, 3, 6, 1, 2, 1, 1, 1, 0), None)],
    cbFun
)

# Run I/O dispatcher which would send pending queries and process responses
snmpEngine.transportDispatcher.runDispatcher()

config.delTransport(
    snmpEngine,
    udp.DOMAIN_NAME
).closeTransport()
Пример #12
0

# Error/response receiver
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus,
          errorIndex, varBinds, cbCtx):
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' %
              (errorStatus.prettyPrint(),
               errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
    else:
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))


# Prepare and send a request message
cmdgen.GetCommandGenerator().sendVarBinds(
    snmpEngine,
    'my-router',
    None,
    '',  # contextEngineId, contextName
    [((1, 3, 6, 1, 2, 1, 1, 1, 0), None)],
    cbFun)

# Run I/O dispatcher which would send pending queries and process responses
snmpEngine.transportDispatcher.runDispatcher()

config.delTransport(snmpEngine, udp.domainName).closeTransport()
    def uncfgCmdGen(self, authData=None):
        if authData:
            if isinstance(authData, CommunityData):
                authDataKey = authData.communityIndex
            elif isinstance(authData, UsmUserData):
                authDataKey = authData.userName, authData.securityEngineId
            else:
                raise error.PySnmpError('Unsupported authentication object')
            if authDataKey in self.__knownAuths:
                authDataKeys = ( authDataKey, )
            else:
                raise error.PySnmpError('Unknown authData %s' % (authData,))
        else:
            authDataKeys = list(self.__knownAuths.keys())

        addrNames, paramsNames = set(), set()

        for authDataKey in authDataKeys:
            authDataX = self.__knownAuths[authDataKey] 
            del self.__knownAuths[authDataKey]
            if isinstance(authDataX, CommunityData):
                config.delV1System(
                    self.snmpEngine,
                    authDataX.communityIndex
                )
            elif isinstance(authDataX, UsmUserData):
                config.delV3User(
                    self.snmpEngine,
                    authDataX.userName, 
                    authDataX.securityEngineId
                )
            else:
                raise error.PySnmpError('Unsupported authentication object')

            paramsKey = authDataX.securityName, \
                        authDataX.securityLevel, \
                        authDataX.mpModel
            if paramsKey in self.__knownParams:
                paramsName, useCount = self.__knownParams[paramsKey]
                useCount -= 1
                if useCount:
                    self.__knownParams[paramsKey] = paramsName, useCount
                else:
                    del self.__knownParams[paramsKey]
                    config.delTargetParams(
                        self.snmpEngine, paramsName
                    )
                    paramsNames.add(paramsName)
            else:
                raise error.PySnmpError('Unknown target %s' % (paramsKey,))

            addrKeys = [ x for x in self.__knownTransportAddrs if x[0] == paramsName ]

            for addrKey in addrKeys:
                addrName, useCount = self.__knownTransportAddrs[addrKey]
                useCount -= 1
                if useCount:
                    self.__knownTransportAddrs[addrKey] = addrName, useCount
                else:
                    config.delTargetAddr(self.snmpEngine, addrName)

                    addrNames.add(addrKey)

                    if addrKey[1] in self.__knownTransports:
                        transport, useCount = self.__knownTransports[addrKey[1]]
                        if useCount > 1:
                            useCount -= 1
                            self.__knownTransports[addrKey[1]] = transport, useCount
                        else:
                            config.delTransport(self.snmpEngine, addrKey[1])
                            transport.closeTransport()
                            del self.__knownTransports[addrKey[1]]

        return addrNames, paramsNames