PySNMP Project Logo

Project diary
Documentation
Examples
License
Download
Mailing lists

Projects using PySNMP
TwistedSNMP
Zenoss


Relevant projects
SNMPy
YAPSNMP
LibSMI
Scotty software

SourceForge Logo

import sys, getopt
from pysnmp.proto import v2c
from pysnmp.mapping.udp import role
import pysnmp.proto.api.generic
import pysnmp.proto.cli.ucd

# Initialize help messages
options =           'Options:\n'
options = options + '  -p       port to communicate with at the agent. Default is 161.\n'
options = options + '  -r    number of retries to be used in requests. Default is 5.\n'
options = options + '  -t    timeout between retries. Default is 1.\n'
options = options + '  -R             report variables types on output.'
usage = 'Usage: %s [options] ' % sys.argv[0]
usage = usage + ' ' + v2c.GetBulkRequest().cliUcdGetUsage() + '\n' + options
    
# Initialize defaults
port = 161; retries = 5; timeout = 1; reportTypeFlag = None

# Parse possible options
try:
    (opts, args) = getopt.getopt(sys.argv[1:], 'hp:r:t:R',\
                                 ['help', 'port=', 'retries=',\
                                  'timeout=', 'report-type'])
except getopt.error, why:
    print 'getopt error: %s\n%s' % (why, usage)
    sys.exit(-1)

try:
    for opt in opts:
        if opt[0] == '-h' or opt[0] == '--help':
            print usage
            sys.exit(0)
        
        if opt[0] == '-p' or opt[0] == '--port':
            port = int(opt[1])

        if opt[0] == '-r' or opt[0] == '--retries':
            retries = int(opt[1])

        if opt[0] == '-t' or opt[0] == '--timeout':
            timeout = int(opt[1])

        if opt[0] == '-R' or opt[0] == '--report-type':
            reportTypeFlag = 1

except ValueError, why:
    print 'Bad parameter \'%s\' for option %s: %s\n%s' \
          % (opt[1], opt[0], why, usage)
    sys.exit(-1)

if len(args) < 1:
    print 'Insufficient number of arguments supplied\n%s' % usage
    sys.exit(-1)

# Create SNMP manager object
client = role.manager((args[0], port))

# Pass it a few options
client.timeout = timeout
client.retries = retries

# Create SNMP GETBULK request
req = v2c.GetBulkRequest()

# Initialize request message from C/L params
req.cliUcdSetArgs(args[1:])

# Create a response message framework
rsp = v2c.Response()

# Store tables headers
headVars = map(lambda x: x[0], req.apiGenGetPdu().apiGenGetVarBind())

# Traverse agent MIB
while 1:
    def cb_fun(answer, src):
        """This is meant to verify inbound messages against out-of-order
           messages
        """
        # Decode message
        rsp.decode(answer)
        
        # Make sure response matches request
        if req.match(rsp):
            return 1
        
    # Encode SNMP request message and try to send it to SNMP agent and
    # receive a response
    (answer, src) = client.send_and_receive(req.encode(), (None, 0), cb_fun)
    
    # Fetch Object ID's and associated values
    vars = rsp.apiGenGetPdu().apiGenGetVarBind()

    # Check for remote SNMP agent failure
    if rsp.apiGenGetPdu().apiGenGetErrorStatus():
        raise str(rsp['pdu'].values()[0]['error_status']) + ' at '\
              + str(vars[rsp.apiGenGetPdu().apiGenGetErrorIndex()-1][0])

    # The following is taken from RFC1905 (fixed not to depend of repetitions)
    N = 0;
    R = len(req.apiGenGetPdu().apiGenGetVarBind()) - N
    M = len(rsp.apiGenGetPdu().apiGenGetVarBind()) / R
    for i in range(1, M+1):
        for r in range(1, R+1):
            idx = N + ((i-1)*R) + r
            (oid, val) = vars[idx-1]
            print oid, ' ---> ',
            if reportTypeFlag:
                print val
            else:
                print repr(val.get())

    # Leave the last of each requested OIDs
    vars = vars[-R:]

    # Exclude completed OIDs
    for idx in range(len(vars)):
        if isinstance(vars[idx][1], v2c.EndOfMibView):
            del vars[idx]; del headVars[idx]
            continue

    if len(vars) == 0:
        sys.exit(0)
        
    # Update request ID
    req.apiGenGetPdu().apiGenSetRequestId(req.apiGenGetPdu().apiGenGetRequestId()+1)    

    # Load get-next'ed vars into new req
    req.apiGenGetPdu().apiGenSetVarBind(vars)

Need help? Try PySNMP mailing lists.