PySNMP: Introduction

### PySNMP introduction

The PySNMP toolkit implements basic components of a SNMP (Simple Network Management Protocol) entity, such as SNMP manager and agent, purely in Python programming language. For more information on SNMP protocol, see RFC 1157, RFC 1155, RFC 1212 and RFC 1213 covering version 1, while RFC 1905, RFC 1902, RFC 1449, RFC 1907 and RFC 1908 are dedicated to version 2.

At the time of this writing, PySNMP implements SNMP protocol processing (v1, v2c) and SNMP message transmission (UDP/IP) facilities.

Here's an example of performing a SNMP GET operation at an interactive Python prompt:

    Python 1.5.2 (#3, Aug 25 1999, 19:14:24)  [GCC 2.8.1] on sunos5
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> from pysnmp.proto.api import alpha
    >>> from pysnmp.mapping.udp import role
    >>>
    >>> ver = alpha.protoVersions[alpha.protoVersionId1]
    >>> req = ver.Message()
    .>> req.apiAlphaSetCommunity('mycomm')
    >>> req.apiAlphaSetPdu(ver.GetRequestPdu())
    >>> req.apiAlphaGetPdu().apiAlphaSetVarBindList(('1.3.6.1.2.1.1.5.0', ver.Null()))
    >>>
    >>> def cbFun(wholeMsg, transportAddr, req):
    >>>     rsp = ver.Message()
    >>>     rsp.berDecode(wholeMsg)
    >>>     if req.apiAlphaMatch(rsp):
    >>>         for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList():
    >>>             print varBind.apiAlphaGetOidVal()
    >>>         return not None
    >>>
    >>> tspDsp = role.Manager()
    >>> tspDsp.sendAndReceive(req.berEncode(), ('rt1.glas.net', 161), (cbFun, req))
    (ObjectName('.1.3.6.1.2.1.1.5.0'), OctetString('router-1'))
    >>>

PySNMP software consists of a few relatively independend components (in fact, Python packages). In the core of the system is a set of general purpose ASN.1 classes upon which SNMP protocol is implemented. Serialized SNMP message can be carried over with a simple set of transport classes also provided with this package.

Here is general layout of the PySNMP software:

    [ pysnmp.asn1.encoding.ber ]
             |
              ---mixin--->[ pysnmp.asn1 ]
                                 |
                 --mixin->[ pysnmp.proto ]
                |                |
     [ pysnmp.proto.api ]        |            [ pysnmp.mapping.udp ]
                                 |                      |
                                  -------        -------
                                         |      |
                                    ------------------
                                   | SNMP application |
                                    ------------------

On the above figure, the pysnmp.asn1 sub-package implements a framework for creating and using various ASN.1 data types, while the pysnmp.asn1.encoding.ber sub-package, being mixed into the pysnmp.asn1, provides one of the possible data serialization methods -- BER (Basic Encoding Rules), which is used in SNMP.

The pysnmp.proto sub-package helds SNMP protocol implementation. Basically, these are the implementations of the Structure of Management Information and Protocol Operations for various SNMP versions. In theory, all these pysnmp.proto.* classes might have been generated automatically by a MIB compiler (which is not yet implemented) from protocol description expressed in ASN.1 language. Meanwhile, SNMP protocol is classes have been coded manually.

A collection of various higher-level, protocol version neutral API's to SNMP protocol classes could be found in the pysnmp.proto.api sub-package.

SNMP specification defines multiple possible network transports to carry SNMP messages over the wire. One of these transports, for TCP/IP networks, is implemented by the pysnmp.mapping.udp sub-package.

What follows is the documentation on all these PySNMP sub-packages.

- Structure for Management Information and Protocol Operations
- Transport mappings for SNMP
- ASN.1 framework
- Compatibility interfaces