Skip to content

Commit

Permalink
Added sync API based on asyncio.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Mar 4, 2024
1 parent 1f087b6 commit 0fa0d27
Show file tree
Hide file tree
Showing 16 changed files with 1,291 additions and 56 deletions.
91 changes: 41 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,62 +86,54 @@ PySNMP is designed in a layered fashion. Top-level and easiest to use API is kno
*hlapi*. Here's a quick example on how to SNMP GET:

```python
import asyncio
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.hlapi import *
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

async def run():
with Slim(1) as slim:
errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)
with Slim(1) as slim:
errorIndication, errorStatus, errorIndex, varBinds = slim.get(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

if errorIndication:
print(errorIndication)
elif errorStatus:
print(
"{} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print(
"{} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
else:
for varBind in varBinds:
print(" = ".join([x.prettyPrint() for x in varBind]))


asyncio.run(run())
)
else:
for varBind in varBinds:
print(" = ".join([x.prettyPrint() for x in varBind]))
```

This is how to send SNMP TRAP:

```python
import asyncio
from pysnmp.hlapi.asyncio import *

async def run():
snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2")).addVarBinds(
("1.3.6.1.6.3.1.1.4.3.0", "1.3.6.1.4.1.20408.4.1.1.2"),
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
),
)

if errorIndication:
print(errorIndication)

snmpEngine.transportDispatcher.closeDispatcher()

asyncio.run(run())
from pysnmp.hlapi import *


snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity("1.3.6.1.6.3.1.1.5.2")).addVarBinds(
("1.3.6.1.6.3.1.1.4.3.0", "1.3.6.1.4.1.20408.4.1.1.2"),
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
),
)

if errorIndication:
print(errorIndication)

snmpEngine.transportDispatcher.closeDispatcher()
```

> We maintain publicly available SNMP Agent and TRAP sink at
Expand All @@ -160,7 +152,7 @@ SNMPv2-MIB::sysName.0 = system name

Other than that, PySNMP is capable to automatically fetch and use required MIBs from HTTP sites
or local directories. You could configure any MIB source available to you (including
[this one](https://github.com/lextudio/mibs.snmplabs.com/tree/master/asn1)) for that purpose.
[this one](https://mibs.pysnmp.com)) for that purpose.

For more sample scripts please refer to [examples section](https://www.pysnmp.com/pysnmp/examples/index.html#high-level-snmp)
at PySNMP web site.
Expand All @@ -172,8 +164,7 @@ Library documentation and examples can be found at the [PySNMP docs site](https:

If something does not work as expected, please
[open an issue](https://github.com/lextudio/pysnmp/issues) at GitHub or
post your question [on Stack Overflow](http://stackoverflow.com/questions/ask) or try browsing pysnmp
[mailing list archives](https://sourceforge.net/p/pysnmp/mailman/pysnmp-users/).
post your question [on Stack Overflow](http://stackoverflow.com/questions/ask).

Bug reports and PRs are appreciated! ;-)

Expand Down
24 changes: 24 additions & 0 deletions docs/source/docs/api-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ Most of SNMP operations involve packet exchange over network. PySNMP
is shipped with asyncio binding that let you run PySNMP in parallel with
other tasks your application may perform.

Synchronous SNMP
----------------

Most simple and strightforward way to use PySNMP is by employing its
Synchronous, blocking API. It's also the default API offered by
users on *pysnmp.hlapi* sub-package import.

Transport configuration
+++++++++++++++++++++++

The following shortcut classes convey configuration information to
SNMP engine's Local Configuration Datastore (:RFC:`2271#section-3.4.2`)
as well as to underlying socket API. Once committed to LCD, SNMP engine
saves its configuration for the lifetime of SNMP engine object.

.. toctree::
:maxdepth: 2

.. autoclass:: pysnmp.hlapi.UdpTransportTarget
:members: setLocalAddress

.. autoclass:: pysnmp.hlapi.Udp6TransportTarget
:members: setLocalAddress

Asynchronous: asyncio
---------------------

Expand Down
2 changes: 1 addition & 1 deletion pysnmp/hlapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pysnmp.entity.engine import *

# default is synchronous asyncio-based API
from pysnmp.hlapi.asyncio import *
from pysnmp.hlapi.asyncio.sync import *


CommunityData = auth.CommunityData
Expand Down
10 changes: 10 additions & 0 deletions pysnmp/hlapi/asyncio/sync/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pysnmp.proto.rfc1902 import *
from pysnmp.smi.rfc1902 import *
from pysnmp.hlapi.auth import *
from pysnmp.hlapi.context import *
from pysnmp.hlapi.asyncio.transport import *
from pysnmp.entity.engine import *


from pysnmp.hlapi.asyncio.sync.cmdgen import *
from pysnmp.hlapi.asyncio.sync.ntforg import *
Loading

0 comments on commit 0fa0d27

Please sign in to comment.