How to Implement the Snmpwalk Utility in Python
The SNMP (Simple Network Management Protocol) protocol is used in network management systems for communication between a server and a remote device. The server is called the manager, and the remote device is referred to as the agent.
The agent in the MIB creates all the information that a manager can read and control. The MIB is a table structure similar to a tree with numbers representing all nodes.
This protocol is not implemented natively by Python. In Python, we use the netsnmp
module, an open-source module providing different tools for emulating various SNMP utilities.
This tutorial will demonstrate Python’s netsnmp
module for the snmpwalk
utility.
Implement the snmpwalk
Utility in Python
We can retrieve SNMP management data using SNMP GETNEXT requests. The snmpwalk
uses these GETNEXT requests to retrieve values from the MIB hierarchy.
With the snmpwalk
application, we run multiple requests to get the values from the whole MIB hierarchy at once.
To implement in Python, we can use the netsnmp.snmpwalk()
function to emulate the snmpwalk
utility. It accepts a VarList
or VarBind
from which the snmpwalk
will begin.
It returns the values from the MIB in a tuple. It is recommended to pass a VarList
instead of a VarBind
because a VarList
is repeatedly updated till it contains the complete set of VarBind
for the results.
If the VarBind
is passed, it is impossible to examine the returned OIDs.
As shown below, we will use this function to emulate snmpwalk
in Python.
import netsnmp
server = "172.xx.x.x"
password = "snmp_private"
oids_varlist = netsnmp.VarList("IF-MIB::ifName", "IF-MIB::ifDescr")
result = netsnmp.snmpwalk(oids_varlist, Version=2, DestHost=server, Community=password)
In the above example, we create a VarList
using the netsnmp.VarList()
constructor. This is passed with the server and password details of the manager in netsnmp.snmpwalk()
to retrieve the network information.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn