From 695e84ab17955fa1a3603f560113bad7f4446716 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sat, 29 Dec 2018 23:45:49 +0100 Subject: Extend copyright notice to year 2019 --- pysnmp/entity/config.py | 2 +- pysnmp/entity/engine.py | 2 +- pysnmp/entity/observer.py | 2 +- pysnmp/entity/rfc3413/cmdgen.py | 2 +- pysnmp/entity/rfc3413/cmdrsp.py | 2 +- pysnmp/entity/rfc3413/config.py | 2 +- pysnmp/entity/rfc3413/context.py | 2 +- pysnmp/entity/rfc3413/mibvar.py | 82 ++++++++++ pysnmp/entity/rfc3413/ntforg.py | 2 +- pysnmp/entity/rfc3413/ntfrcv.py | 2 +- pysnmp/entity/rfc3413/oneliner/cmdgen.py | 254 +++++++++++++++++++++++++++++++ pysnmp/entity/rfc3413/oneliner/ntforg.py | 180 ++++++++++++++++++++++ 12 files changed, 525 insertions(+), 9 deletions(-) create mode 100644 pysnmp/entity/rfc3413/mibvar.py create mode 100644 pysnmp/entity/rfc3413/oneliner/cmdgen.py create mode 100644 pysnmp/entity/rfc3413/oneliner/ntforg.py (limited to 'pysnmp/entity') diff --git a/pysnmp/entity/config.py b/pysnmp/entity/config.py index 6aed74c4..4b285538 100644 --- a/pysnmp/entity/config.py +++ b/pysnmp/entity/config.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # from pyasn1.compat.octets import null diff --git a/pysnmp/entity/engine.py b/pysnmp/entity/engine.py index 0550871b..a299267c 100644 --- a/pysnmp/entity/engine.py +++ b/pysnmp/entity/engine.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # import os diff --git a/pysnmp/entity/observer.py b/pysnmp/entity/observer.py index a5239471..3f7bd845 100644 --- a/pysnmp/entity/observer.py +++ b/pysnmp/entity/observer.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # from pysnmp import error diff --git a/pysnmp/entity/rfc3413/cmdgen.py b/pysnmp/entity/rfc3413/cmdgen.py index 9f5d891d..98cac48c 100644 --- a/pysnmp/entity/rfc3413/cmdgen.py +++ b/pysnmp/entity/rfc3413/cmdgen.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # import sys diff --git a/pysnmp/entity/rfc3413/cmdrsp.py b/pysnmp/entity/rfc3413/cmdrsp.py index 0aa3b1e1..772cc5ea 100644 --- a/pysnmp/entity/rfc3413/cmdrsp.py +++ b/pysnmp/entity/rfc3413/cmdrsp.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # import sys diff --git a/pysnmp/entity/rfc3413/config.py b/pysnmp/entity/rfc3413/config.py index 5562784c..144aa486 100644 --- a/pysnmp/entity/rfc3413/config.py +++ b/pysnmp/entity/rfc3413/config.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # from pysnmp.smi.error import SmiError, NoSuchInstanceError diff --git a/pysnmp/entity/rfc3413/context.py b/pysnmp/entity/rfc3413/context.py index ec705a72..3975e670 100644 --- a/pysnmp/entity/rfc3413/context.py +++ b/pysnmp/entity/rfc3413/context.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # from pyasn1.type import univ diff --git a/pysnmp/entity/rfc3413/mibvar.py b/pysnmp/entity/rfc3413/mibvar.py new file mode 100644 index 00000000..bfc63a06 --- /dev/null +++ b/pysnmp/entity/rfc3413/mibvar.py @@ -0,0 +1,82 @@ +# +# This file is part of pysnmp software. +# +# Copyright (c) 2005-2019, Ilya Etingof +# License: http://snmplabs.com/pysnmp/license.html +# +# THESE FUNCTIONS ARE OBSOLETE AND MUST NOT BE USED! +# USE pysnmp.entity.rfc3413.oneliner.mibvar INSTEAD +# +from pyasn1.type import univ +from pysnmp.smi.error import NoSuchObjectError + + +# Name + +def mibNameToOid(mibView, name): + if isinstance(name[0], tuple): + f = lambda x='', y='': (x, y) + modName, symName = f(*name[0]) + if modName: # load module if needed + mibView.mibBuilder.loadModules(modName) + else: + mibView.mibBuilder.loadModules() # load all (slow) + if symName: + oid, label, suffix = mibView.getNodeNameByDesc(symName, modName) + else: + oid, label, suffix = mibView.getFirstNodeName(modName) + suffix = name[1:] + modName, symName, _s = mibView.getNodeLocation(oid) + mibNode, = mibView.mibBuilder.importSymbols( + modName, symName + ) + if hasattr(mibNode, 'createTest'): # table column XXX + modName, symName, _s = mibView.getNodeLocation(oid[:-1]) + rowNode, = mibView.mibBuilder.importSymbols(modName, symName) + return oid, rowNode.getInstIdFromIndices(*suffix) + else: # scalar or incomplete spec + return oid, suffix + elif not isinstance(name, tuple): + name = tuple(univ.ObjectIdentifier(name)) + + oid, label, suffix = mibView.getNodeNameByOid(name) + + return oid, suffix + + +__scalarSuffix = (univ.Integer(0),) + + +def oidToMibName(mibView, oid): + if not isinstance(oid, tuple): + oid = tuple(univ.ObjectIdentifier(oid)) + _oid, label, suffix = mibView.getNodeNameByOid(oid) + modName, symName, __suffix = mibView.getNodeLocation(_oid) + mibNode, = mibView.mibBuilder.importSymbols( + modName, symName + ) + if hasattr(mibNode, 'createTest'): # table column + __modName, __symName, __s = mibView.getNodeLocation(_oid[:-1]) + rowNode, = mibView.mibBuilder.importSymbols(__modName, __symName) + return (symName, modName), rowNode.getIndicesFromInstId(suffix) + elif not suffix: # scalar + return (symName, modName), suffix + elif suffix == (0,): # scalar + return (symName, modName), __scalarSuffix + else: + raise NoSuchObjectError( + str='No MIB registered that defines %s object, closest known parent is %s (%s::%s)' % ( + univ.ObjectIdentifier(oid), univ.ObjectIdentifier(mibNode.name), modName, symName) + ) + + +# Value + +def cloneFromMibValue(mibView, modName, symName, value): + mibNode, = mibView.mibBuilder.importSymbols( + modName, symName + ) + if hasattr(mibNode, 'syntax'): # scalar + return mibNode.syntax.clone(value) + else: + return # identifier diff --git a/pysnmp/entity/rfc3413/ntforg.py b/pysnmp/entity/rfc3413/ntforg.py index 85bdb9d0..19442b3a 100644 --- a/pysnmp/entity/rfc3413/ntforg.py +++ b/pysnmp/entity/rfc3413/ntforg.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # import sys diff --git a/pysnmp/entity/rfc3413/ntfrcv.py b/pysnmp/entity/rfc3413/ntfrcv.py index df394a0c..fbec47ca 100644 --- a/pysnmp/entity/rfc3413/ntfrcv.py +++ b/pysnmp/entity/rfc3413/ntfrcv.py @@ -1,7 +1,7 @@ # # This file is part of pysnmp software. # -# Copyright (c) 2005-2018, Ilya Etingof +# Copyright (c) 2005-2019, Ilya Etingof # License: http://snmplabs.com/pysnmp/license.html # import sys diff --git a/pysnmp/entity/rfc3413/oneliner/cmdgen.py b/pysnmp/entity/rfc3413/oneliner/cmdgen.py new file mode 100644 index 00000000..b74b7f13 --- /dev/null +++ b/pysnmp/entity/rfc3413/oneliner/cmdgen.py @@ -0,0 +1,254 @@ +# +# This file is part of pysnmp software. +# +# Copyright (c) 2005-2019, Ilya Etingof +# License: http://snmplabs.com/pysnmp/license.html +# +# All code in this file belongs to obsolete, compatibility wrappers. +# Never use interfaces below for new applications! +# +from pysnmp.hlapi.asyncore import * +from pysnmp.hlapi.asyncore import sync +from pysnmp.hlapi.varbinds import * +from pysnmp.hlapi.lcd import * +from pyasn1.compat.octets import null +from pyasn1.type import univ + +__all__ = ['AsynCommandGenerator', 'CommandGenerator', 'MibVariable'] + +MibVariable = ObjectIdentity + + +class AsynCommandGenerator(object): + _null = univ.Null('') + + vbProcessor = CommandGeneratorVarBinds() + lcd = CommandGeneratorLcdConfigurator() + + def __init__(self, snmpEngine=None): + if snmpEngine is None: + self.snmpEngine = SnmpEngine() + else: + self.snmpEngine = snmpEngine + + self.mibViewController = self.vbProcessor.getMibViewController(self.snmpEngine) + + def __del__(self): + self.lcd.unconfigure(self.snmpEngine) + + def cfgCmdGen(self, authData, transportTarget): + return self.lcd.configure(self.snmpEngine, authData, transportTarget) + + def uncfgCmdGen(self, authData=None): + return self.lcd.unconfigure(self.snmpEngine, authData) + + # compatibility stub + def makeReadVarBinds(self, varNames): + return self.makeVarBinds([(x, self._null) for x in varNames]) + + def makeVarBinds(self, varBinds): + return self.vbProcessor.makeVarBinds(self.snmpEngine, varBinds) + + def unmakeVarBinds(self, varBinds, lookupNames, lookupValues): + return self.vbProcessor.unmakeVarBinds( + self.snmpEngine, varBinds, lookupNames or lookupValues + ) + + def getCmd(self, authData, transportTarget, varNames, cbInfo, + lookupNames=False, lookupValues=False, + contextEngineId=None, contextName=null): + + def __cbFun(snmpEngine, sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbInfo): + cbFun, cbCtx = cbInfo + cbFun(sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbCtx) + + # for backward compatibility + if contextName is null and authData.contextName: + contextName = authData.contextName + + return getCmd( + self.snmpEngine, authData, transportTarget, + ContextData(contextEngineId, contextName), + *[(x, self._null) for x in varNames], + **dict(cbFun=__cbFun, cbCtx=cbInfo, + lookupMib=lookupNames or lookupValues) + ) + + asyncGetCmd = getCmd + + def setCmd(self, authData, transportTarget, varBinds, cbInfo, + lookupNames=False, lookupValues=False, + contextEngineId=None, contextName=null): + + def __cbFun(snmpEngine, sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbInfo): + cbFun, cbCtx = cbInfo + cbFun(sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbCtx) + + # for backward compatibility + if contextName is null and authData.contextName: + contextName = authData.contextName + + return setCmd( + self.snmpEngine, authData, transportTarget, + ContextData(contextEngineId, contextName), *varBinds, + **dict(cbFun=__cbFun, cbCtx=cbInfo, + lookupMib=lookupNames or lookupValues) + ) + + asyncSetCmd = setCmd + + def nextCmd(self, authData, transportTarget, varNames, cbInfo, + lookupNames=False, lookupValues=False, + contextEngineId=None, contextName=null): + + def __cbFun(snmpEngine, sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbInfo): + cbFun, cbCtx = cbInfo + return cbFun(sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbCtx) + + # for backward compatibility + if contextName is null and authData.contextName: + contextName = authData.contextName + + return nextCmd( + self.snmpEngine, authData, transportTarget, + ContextData(contextEngineId, contextName), + *[(x, self._null) for x in varNames], + **dict(cbFun=__cbFun, cbCtx=cbInfo, + lookupMib=lookupNames or lookupValues) + ) + + asyncNextCmd = nextCmd + + def bulkCmd(self, authData, transportTarget, + nonRepeaters, maxRepetitions, varNames, cbInfo, + lookupNames=False, lookupValues=False, + contextEngineId=None, contextName=null): + + def __cbFun(snmpEngine, sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbInfo): + cbFun, cbCtx = cbInfo + return cbFun(sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbCtx) + + # for backward compatibility + if contextName is null and authData.contextName: + contextName = authData.contextName + + return bulkCmd( + self.snmpEngine, authData, transportTarget, + ContextData(contextEngineId, contextName), + nonRepeaters, maxRepetitions, + *[(x, self._null) for x in varNames], + **dict(cbFun=__cbFun, cbCtx=cbInfo, + lookupMib=lookupNames or lookupValues) + ) + + asyncBulkCmd = bulkCmd + + +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 diff --git a/pysnmp/entity/rfc3413/oneliner/ntforg.py b/pysnmp/entity/rfc3413/oneliner/ntforg.py new file mode 100644 index 00000000..0f0d2d2e --- /dev/null +++ b/pysnmp/entity/rfc3413/oneliner/ntforg.py @@ -0,0 +1,180 @@ +# +# This file is part of pysnmp software. +# +# Copyright (c) 2005-2019, Ilya Etingof +# License: http://snmplabs.com/pysnmp/license.html +# +# All code in this file belongs to obsolete, compatibility wrappers. +# Never use interfaces below for new applications! +# +from pysnmp.hlapi.asyncore import * +from pysnmp.hlapi.asyncore import sync +from pysnmp.hlapi.varbinds import * +from pysnmp.hlapi.lcd import * +from pyasn1.compat.octets import null +from pysnmp.entity import config +from pysnmp.entity.rfc3413 import context + +__all__ = ['AsynNotificationOriginator', + 'NotificationOriginator', + 'MibVariable'] + +MibVariable = ObjectIdentity + + +class ErrorIndicationReturn(object): + def __init__(self, *vars): + self.__vars = vars + + def __getitem__(self, i): + return self.__vars[i] + + def __nonzero__(self): + return bool(self) + + def __bool__(self): + return bool(self.__vars[0]) + + def __str__(self): + return str(self.__vars[0]) + + +class AsynNotificationOriginator(object): + vbProcessor = NotificationOriginatorVarBinds() + lcd = NotificationOriginatorLcdConfigurator() + + def __init__(self, snmpEngine=None, snmpContext=None): + if snmpEngine is None: + self.snmpEngine = snmpEngine = SnmpEngine() + else: + self.snmpEngine = snmpEngine + + if snmpContext is None: + self.snmpContext = context.SnmpContext(self.snmpEngine) + config.addContext( + self.snmpEngine, '' # this is leaky + ) + else: + self.snmpContext = snmpContext + + self.mibViewController = self.vbProcessor.getMibViewController(self.snmpEngine) + + def __del__(self): + self.uncfgNtfOrg() + + def cfgNtfOrg(self, authData, transportTarget, notifyType): + return self.lcd.configure( + self.snmpEngine, authData, transportTarget, notifyType + ) + + def uncfgNtfOrg(self, authData=None): + return self.lcd.unconfigure(self.snmpEngine, authData) + + def makeVarBinds(self, varBinds): + return self.vbProcessor.makeVarBinds( + self.snmpEngine, varBinds + ) + + def unmakeVarBinds(self, varBinds, lookupNames, lookupValues): + return self.vbProcessor.unmakeVarBinds( + self.snmpEngine, varBinds, lookupNames or lookupValues + ) + + def sendNotification(self, authData, transportTarget, + notifyType, notificationType, + varBinds=(), # legacy, use NotificationType instead + cbInfo=(None, None), + lookupNames=False, lookupValues=False, + contextEngineId=None, # XXX ordering incompatibility + contextName=null): + + def __cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + cbFun, cbCtx = cbCtx + try: + # we need to pass response PDU information to user for INFORMs + return cbFun and cbFun( + sendRequestHandle, + errorIndication, + errorStatus, errorIndex, + varBinds, + cbCtx + ) + except TypeError: + # a backward compatible way of calling user function + return cbFun( + sendRequestHandle, + errorIndication, + cbCtx + ) + + # for backward compatibility + if contextName is null and authData.contextName: + contextName = authData.contextName + + if not isinstance(notificationType, + (ObjectIdentity, ObjectType, NotificationType)): + if isinstance(notificationType[0], tuple): + # legacy + notificationType = ObjectIdentity(notificationType[0][0], notificationType[0][1], *notificationType[1:]) + else: + notificationType = ObjectIdentity(notificationType) + + if not isinstance(notificationType, NotificationType): + notificationType = NotificationType(notificationType) + + return sendNotification( + self.snmpEngine, + authData, transportTarget, + ContextData(contextEngineId or self.snmpContext.contextEngineId, + contextName), + notifyType, notificationType.addVarBinds(*varBinds), + __cbFun, + cbInfo, + lookupNames or lookupValues + ) + + asyncSendNotification = sendNotification + + +class NotificationOriginator(object): + vbProcessor = NotificationOriginatorVarBinds() + + def __init__(self, snmpEngine=None, snmpContext=None, asynNtfOrg=None): + # compatibility attributes + self.snmpEngine = snmpEngine or SnmpEngine() + self.mibViewController = self.vbProcessor.getMibViewController(self.snmpEngine) + + # the varBinds parameter is legacy, use NotificationType instead + + def sendNotification(self, authData, transportTarget, notifyType, + notificationType, *varBinds, **kwargs): + if 'lookupNames' not in kwargs: + kwargs['lookupNames'] = False + if 'lookupValues' not in kwargs: + kwargs['lookupValues'] = False + if not isinstance(notificationType, + (ObjectIdentity, ObjectType, NotificationType)): + if isinstance(notificationType[0], tuple): + # legacy + notificationType = ObjectIdentity(notificationType[0][0], notificationType[0][1], *notificationType[1:]) + else: + notificationType = ObjectIdentity(notificationType) + + if not isinstance(notificationType, NotificationType): + notificationType = NotificationType(notificationType) + + for (errorIndication, + errorStatus, + errorIndex, + rspVarBinds) in sync.sendNotification(self.snmpEngine, authData, + transportTarget, + ContextData(kwargs.get('contextEngineId'), + kwargs.get('contextName', null)), + notifyType, + notificationType.addVarBinds(*varBinds), + **kwargs): + if notifyType == 'inform': + return errorIndication, errorStatus, errorIndex, rspVarBinds + else: + break -- cgit v1.2.1