Skip to content

Commit

Permalink
Fixed etingof#33.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Aug 26, 2024
1 parent 40e76ed commit 132ae77
Show file tree
Hide file tree
Showing 43 changed files with 185 additions and 151 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
await UdpTransportTarget.create(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2")).addVarBinds(
Expand Down
26 changes: 13 additions & 13 deletions docs/source/docs/pysnmp-hlapi-tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ SNMP LCD in form of properly initialized
>>>
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
...
Addressing SNMP Context
Expand All @@ -141,7 +141,7 @@ For this example we will use the 'empty' context (default).
>>>
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
...
Expand Down Expand Up @@ -240,7 +240,7 @@ in `RFC3418`_ ``SNMPv2-MIB`` module.
>>> from pysnmp.hlapi.v3arch.asyncio import *
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
Expand Down Expand Up @@ -274,7 +274,7 @@ out, response is awaited, received and parsed.
>>>
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysUpTime', 0)))
>>> g
Expand Down Expand Up @@ -370,7 +370,7 @@ Let's read TCP-MIB::tcpConnectionState object for a TCP connection:
>>>
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('TCP-MIB', 'tcpConnectionState',
... 'ipv4', '195.218.254.105', 41511,
Expand All @@ -392,7 +392,7 @@ function.
>>> from pysnmp.hlapi.v3arch.asyncio import *
>>> g = await nextCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')))
>>> g
Expand All @@ -419,7 +419,7 @@ API as *getNext()* for convenience.
>>> N, R = 0, 25
>>> g = await bulkCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... N, R,
... ObjectType(ObjectIdentity('1.3.6')))
Expand All @@ -440,7 +440,7 @@ of MIB objects.
>>>
>>> g = await nextCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('IF-MIB', 'ifTable')))
>>>
Expand All @@ -457,7 +457,7 @@ values in exactly the same order as they were in request message.
>>>
>>> g = await getCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysUpTime', 0))
Expand All @@ -480,7 +480,7 @@ function.
>>>
>>> g = await setCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0), 'Linux i386')
... )
Expand Down Expand Up @@ -544,7 +544,7 @@ or acknowledgement is sent.
>>>
>>> g = await sendNotification(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 162)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 162)),
... ContextData(),
... 'trap',
... NotificationType(ObjectIdentity('IF-MIB', 'linkUp'), instanceIndex=(123,))
Expand All @@ -562,7 +562,7 @@ well as for agent-to-manager.
>>>
>>> g = await sendNotification(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 162)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 162)),
... ContextData(),
... 'inform',
... NotificationType(ObjectIdentity('IF-MIB', 'linkUp'), instanceIndex=(123,))
Expand Down Expand Up @@ -592,7 +592,7 @@ object OIDs to current values.
>>>
>>> g = await sendNotification(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 162)),
... await UdpTransportTarget.create(('demo.pysnmp.com', 162)),
... ContextData(),
... 'inform',
... NotificationType(ObjectIdentity('IF-MIB', 'linkUp'), instanceIndex=(123,), objects=mib)
Expand Down
8 changes: 4 additions & 4 deletions docs/source/faq/oids-not-increasing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ A. The Agent you are talking to seems to be broken. The

If you have to work with a broken Agent and can terminate the
GETNEXT/GETBULK command at some point, you can pass the
`ignoreNonIncreasingOid=True` keyword parameter to the `nextCmd()` or `bulkCmd()`
`ignoreNonIncreasingOid=True` keyword parameter to the `walkCmd()` or `bulkWalkCmd()`
to disable OID verification at the Manager side.

.. code-block:: python
snmpEngine = SnmpEngine()
for (errorIndication,
async for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(snmpEngine,
varBinds) in walkCmd(snmpEngine,
CommunityData('public'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
await UdpTransportTarget.create(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6')),
ignoreNonIncreasingOid=True):
Expand Down
22 changes: 11 additions & 11 deletions docs/source/faq/snmp-data-constraints-verification-failure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
SNMP data constraints verification error
----------------------------------------

Q. Will PySNMP Manager verify the values it sends to and receives from
Q. Will PySNMP Manager verify the values it sends to and receives from
a distant Agent against local MIB constraints?

A. Yes, it can do that. The Manager will verify the values you pass to SET
request against a MIB if:

The values are not already PyASN1 objects but some basic Python types
(like integer or string). You tell PySNMP engine to load appropriate
The values are not already PyASN1 objects but some basic Python types
(like integer or string). You tell PySNMP engine to load appropriate
MIB where it could lookup the constraints (via the use of MibVariable)
So, the following code fragment makes PySNMP engine loading SNMPv2-MIB
and verifying that the 'new system name' value satisfies sysName
and verifying that the 'new system name' value satisfies sysName
constraints (if any).

.. code-block:: python
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd(
errorIndication, errorStatus, errorIndex, varBinds = await cmdGen.setCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget(('localhost', 161)),
await cmdgen.UdpTransportTarget(('localhost', 161)),
( cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new system name' )
)
To verify the response values, you should pass at least lookupValues flag
to CommandGenerator \*cmd() method you use. In the following example
PySNMP will make sure that Agent-supplied value for SNMPv2-MIB::sysName
To verify the response values, you should pass at least lookupValues flag
to CommandGenerator \*cmd() method you use. In the following example
PySNMP will make sure that Agent-supplied value for SNMPv2-MIB::sysName
Managed Object satisfies MIB constraints (if any).

.. code-block:: python
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await cmdGen.getCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget(('localhost', 161)),
await cmdgen.UdpTransportTarget(('localhost', 161)),
cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0),
lookupValues=True
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def run():
iterator = await sendNotification(
snmpDispatcher,
CommunityData("public", mpModel=0),
UdpTransportTarget(("demo.pysnmp.com", 162)),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
.loadMibs("SNMPv2-MIB")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def sendone(snmpDispatcher, hostname, notifyType):
iterator = await sendNotification(
snmpDispatcher,
CommunityData("public"),
UdpTransportTarget((hostname, 162)),
await UdpTransportTarget.create((hostname, 162)),
notifyType,
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
.loadMibs("SNMPv2-MIB")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def run(varBinds):
iterator = await bulkCmd(
snmpDispatcher,
CommunityData("public"),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
0,
50,
*varBinds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def getone(snmpDispatcher, hostname):
iterator = await getCmd(
snmpDispatcher,
CommunityData("public"),
UdpTransportTarget(hostname),
await UdpTransportTarget.create(hostname),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def getone(snmpDispatcher, hostname):
iterator = await getCmd(
snmpDispatcher,
CommunityData("public"),
UdpTransportTarget(hostname),
await UdpTransportTarget.create(hostname),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/hlapi/v1arch/asyncio/manager/cmdgen/v1-get.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def run():
iterator = await getCmd(
snmpDispatcher,
CommunityData("public", mpModel=0),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
("1.3.6.1.2.1.1.1.0", None),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def run():
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData("public", mpModel=0),
UdpTransportTarget(("demo.pysnmp.com", 162)),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def sendone(snmpEngine, hostname, notifyType):
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData("public", tag=hostname),
UdpTransportTarget((hostname, 162), tagList=hostname),
await UdpTransportTarget.create((hostname, 162), tagList=hostname),
ContextData(),
notifyType,
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
Expand Down
2 changes: 1 addition & 1 deletion examples/hlapi/v3arch/asyncio/agent/ntforg/send-trap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def run():
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData("public", mpModel=0),
UdpTransportTarget(("demo.pysnmp.com", 162)),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
"trap",
NotificationType(
Expand Down
2 changes: 1 addition & 1 deletion examples/hlapi/v3arch/asyncio/agent/ntforg/v3-inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def run():
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
UsmUserData("usr-md5-des", "authkey1", "privkey1"),
UdpTransportTarget(("demo.pysnmp.com", 162)),
await UdpTransportTarget.create(("demo.pysnmp.com", 162)),
ContextData(),
"inform",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def run():
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData("public"),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(
ObjectIdentity("IF-MIB", "ifInOctets", 1).addAsn1MibSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def run(varBinds):
errorIndication, errorStatus, errorIndex, varBindTable = await bulkCmd(
snmpEngine,
UsmUserData("usr-none-none"),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
0,
50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def getone(snmpEngine, hostname):
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData("public"),
UdpTransportTarget(hostname),
await UdpTransportTarget.create(hostname),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def getone(snmpEngine, hostname):
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData("public"),
UdpTransportTarget(hostname),
await UdpTransportTarget.create(hostname),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def run_snmp_get():
privKeyType=USM_KEY_TYPE_LOCALIZED,
securityEngineId=OctetString(hexValue="80004fb805636c6f75644dab22cc"),
),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def run_snmp_get():
authKeyType=USM_KEY_TYPE_MASTER,
privKeyType=USM_KEY_TYPE_MASTER,
),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def run():
authProtocol=USM_AUTH_HMAC96_SHA,
privProtocol=USM_PRIV_CFB128_AES,
),
UdpTransportTarget(("demo.pysnmp.com", 161)),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
Expand Down
17 changes: 14 additions & 3 deletions pysnmp/hlapi/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@ class AbstractTransportTarget:

def __init__(
self,
transportAddr: Tuple,
timeout: float = 1,
retries: int = 5,
tagList=null,
):
self.transportAddr = self._resolveAddr(transportAddr)
self.timeout = timeout
self.retries = retries
self.tagList = tagList
self.iface = None
self.transport = None

if not hasattr(self, "transportAddr"):
raise Exception(
f"Please call .create() to construct {self.__class__.__name__} object"
)

@classmethod
async def create(cls, *args, **kwargs):
self = cls.__new__(cls)
transportAddr = args[0]
self.transportAddr = await self._resolveAddr(transportAddr)
self.__init__(*args[1:], **kwargs)
return self

def __repr__(self):
return "{}({!r}, timeout={!r}, retries={!r}, tagList={!r})".format(
self.__class__.__name__,
Expand Down Expand Up @@ -81,5 +92,5 @@ def verifyDispatcherCompatibility(self, snmpEngine: SnmpEngine):
)
)

def _resolveAddr(self, transportAddr: Tuple) -> Tuple[str, int]:
async def _resolveAddr(self, transportAddr: Tuple) -> Tuple[str, int]:
raise NotImplementedError()
Loading

0 comments on commit 132ae77

Please sign in to comment.