Skip to content

Commit

Permalink
Updated examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 12, 2023
1 parent 48c2b5a commit 0569329
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

async def run():
snmpEngine = SnmpEngine()
trap_result = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
Expand All @@ -37,7 +37,7 @@ async def run():
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
),
)
errorIndication, errorStatus, errorIndex, varBinds = await trap_result

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


async def sendone(snmpEngine, hostname, notifyType):
trap_result = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData("public", tag=hostname),
UdpTransportTarget((hostname, 162), tagList=hostname),
Expand All @@ -39,7 +39,6 @@ async def sendone(snmpEngine, hostname, notifyType):
),
)

(errorIndication, errorStatus, errorIndex, varBinds) = await trap_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
4 changes: 1 addition & 3 deletions examples/hlapi/asyncio/agent/ntforg/v3-inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pysnmp.hlapi.asyncio.transport import UdpTransportTarget

async def run():
iterator = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
SnmpEngine(),
UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
UdpTransportTarget(('demo.pysnmp.com', 162)),
Expand All @@ -33,8 +33,6 @@ async def run():
)
)

errorIndication, errorStatus, errorIndex, varBinds = await iterator

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

async def run():
snmpEngine = SnmpEngine()
get_result = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData('public'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
Expand All @@ -29,7 +29,6 @@ async def run():
'https://mibs.pysnmp.com/asn1/@mib@'))
)

errorIndication, errorStatus, errorIndex, varBinds = await get_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
4 changes: 2 additions & 2 deletions examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
async def run(varBinds):
snmpEngine = SnmpEngine()
while True:
bulk_task = await bulkCmd(
errorIndication, errorStatus, errorIndex, varBindTable = await bulkCmd(
snmpEngine,
UsmUserData('usr-none-none'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
Expand All @@ -33,7 +33,7 @@ async def run(varBinds):
50,
*varBinds
)
(errorIndication, errorStatus, errorIndex, varBindTable) = await bulk_task

if errorIndication:
print(errorIndication)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@


async def getone(snmpEngine, hostname):
result_get = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData("public"),
UdpTransportTarget(hostname),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

errorIndication, errorStatus, errorIndex, varBinds = await result_get
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
26 changes: 26 additions & 0 deletions examples/hlapi/asyncio/manager/cmdgen/send-trap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio
from pysnmp.hlapi.asyncio import *

async def run():
snmpEngine = SnmpEngine()
# Example of how you might update sysUpTime
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
sysUpTime.syntax = TimeTicks(12345) # Set uptime to 12345

errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)

if errorIndication:
print(errorIndication)

snmpEngine.transportDispatcher.closeDispatcher()


asyncio.run(run())
3 changes: 1 addition & 2 deletions examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

async def run():
snmpEngine = SnmpEngine()
get_result = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
UsmUserData('usr-sha-aes', 'authkey1', 'privkey1',
authProtocol=usmHMACSHAAuthProtocol,
Expand All @@ -38,7 +38,6 @@ async def run():
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

errorIndication, errorStatus, errorIndex, varBinds = await get_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
16 changes: 4 additions & 12 deletions pysnmp/hlapi/asyncio/slim.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,14 @@ async def get(self, communityName, address, port, *varBinds):
"""

get_result = await getCmd(
return await getCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await get_result

async def next(self, communityName, address, port, *varBinds):
r"""Creates a generator to perform SNMP GETNEXT query.
Expand Down Expand Up @@ -180,16 +178,14 @@ async def next(self, communityName, address, port, *varBinds):
"""

next_result = await nextCmd(
return await nextCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await next_result

async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions, *varBinds):
r"""Creates a generator to perform SNMP GETBULK query.
Expand Down Expand Up @@ -288,7 +284,7 @@ async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions,
version = self.version - 1
if version == 0:
raise PySnmpError('Cannot send V2 PDU on V1 session')
bulk_result = await bulkCmd(
return await bulkCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=version),
UdpTransportTarget((address, port)),
Expand All @@ -298,8 +294,6 @@ async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions,
*varBinds,
)

return await bulk_result

async def set(self, communityName, address, port, *varBinds):
r"""Creates a generator to perform SNMP SET query.
Expand Down Expand Up @@ -361,12 +355,10 @@ async def set(self, communityName, address, port, *varBinds):
"""

set_result = await setCmd(
return await setCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await set_result

0 comments on commit 0569329

Please sign in to comment.