Introduction to pysnmp

PySNMP is a cross-platform, pure Python SNMP engine implementation (https://github.com/etingof/pysnmp) that abstracts a lot of SNMP details for developers, and supports both Python 2 and Python 3.

You can install the pysnmp module by using the pip command:

$ pip install pysnmp

ASN.1 (https://asn1js.org) is a standard and notation that describes rules and structures to represent, encode, transmit, and decode data in telecommunication and computer networking. PySNMP also requires the PyASN1 package. PyASN1 (https://github.com/etingof/pyasn1) conveniently provides a Python wrapper around ASN.

This module provides a useful wrapper for the snmp commands. Let's learn how to create an snmpwalk command. To begin, import a command generator:

from pysnmp.entity.rfc3413.oneliner import cmdgen
cmd_generator = cmdgen.CommandGenerator()

Then, define the necessary default values for the connection, assuming that the snmpd daemon has been running on port 161 in public SNMP simulator at demo.snmplabs.com and that the community string has been set to public:

SNMP_HOST = 'demo.snmplabs.com'
SNMP_PORT = 161
SNMP_COMMUNITY = 'public'

We can perform SNMP using the getCmd() method. The result is unpacked into various variables. The output of this command consists of a four-value tuple. Out of those, three are related to the errors that are returned by the command generator, and the fourth one (varBinds) is related to the actual variables that bind the returned data and contains the query result:

error_notify, error_status, error_index, var_binds =
cmd_generator.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0),
lookupNames=True, lookupValues=True

You can see that cmdgen takes the following parameters:

  • CommunityData(): Sets the community string as public.
  • UdpTransportTarget(): This is the host target, where the snmp agent is running. This is specified in a pair of the hostname and the UDP port.
  • MibVariable: This is a tuple of values that includes the MIB version number and the MIB target string (which is sysDescr; this refers to the description of the system).

The output of this command consists of a four-value tuple. Out of those, three are related to the errors returned by the command generator, and the fourth is related to the actual variables that bind the returned data. The following example shows how the preceding method can be used to fetch the SNMP host description string from a running SNMP daemon.

You can find the following code in the  snmp_get_information.py file:

#!/usr/bin/env python3

from pysnmp.hlapi import *
import sys

def get_info_snmp(host, oid):
for (errorIndication,errorStatus,errorIndex,varBinds) in nextCmd(SnmpEngine(),
CommunityData('public'),UdpTransportTarget((host, 161)),ContextData(), ObjectType(ObjectIdentity(oid)),lookupMib=False,lexicographicMode=False):

if errorIndication:
print(errorIndication, file=sys.stderr)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), file=sys.stderr)
break
else:
for varBind in varBinds:
print('%s = %s' % varBind)

get_info_snmp('demo.snmplabs.com', '1.3.6.1.2.1.1.9.1.2')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset