pysnmp.sourceforge.net
HOME QUICK START DOCUMENTATION EXAMPLES DOWNLOAD
DEVELOPMENT | CONTACT |

High-level SNMP

Command Generator
Notification Originator

SNMP with Asyncore

Command Generator
Command Responder
Notification Originator
Notification Receiver
Command Forwarder

SNMP with Asyncio

Command Generator
Command Responder
Notification Originator
Notification Receiver

SNMP with Trollius

Command Generator
Notification Originator

SNMP with Twisted

Command Generator
Command Responder
Notification Originator
Notification Receiver

Packet-level SNMP

Command Generator
Command Responder
Notification Originator
Notification Receiver

WARNING: you are reading historical documentation! Please, refer here.

High-level SNMP API

This chapter illustrates various uses of the high-level programming interfaces to some of Standard SNMP Applicaitons, as defined in RFC3413. The so called "oneliner" API is designed to be simple, concise and suitable for the most frequent operations. For that matter only Command Generator and Notification Originator Applications are currently wrapped into a nearly one-line Python expression.

More sophisticated or less popular SNMP operations can still be performed with PySNMP through its Native API to Standard SNMP Applications.

The following code performs SNMP GET operation

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
    cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0)
)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

To make use of SNMPv3 and USM, the following code performs SNMP GETNEXT operation

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.UsmUserData('usr-md5-none', 'authkey1'),
    cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
    cmdgen.MibVariable('SNMPv2-MIB', 'sysContact', '0')
)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

More examples on oneliner API use with SNMP Applications:

Sending SNMP TRAP's and INFORM's is as easy with PySNMP's oneliner library. The following code sends SNMP TRAP:

from pysnmp.entity.rfc3413.oneliner import ntforg

ntfOrg = ntforg.NotificationOriginator()

errorIndication = ntfOrg.sendNotification(
    ntforg.CommunityData('public'),
    ntforg.UdpTransportTarget(('demo.snmplabs.com', 162)),
    'trap',
    ntforg.MibVariable('SNMPv2-MIB', 'coldStart'),
    (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')
)

if errorIndication:
    print('Notification not sent: %s' % errorIndication)

More examples on oneliner API use with SNMP Applications:

For more details on PySNMP programming model and interfaces, please refer to the documentation.

To download this example as a Python file, please click here.


Need help? Try PySNMP mailing lists or report to library maintainers.
SourceForge Logo