TUT:snmpget
The GET request is one of the basic operations of the SNMP protocol, retrieving the information associated with the specified OID from the target agent:
% snmpget -v 1 -c demopublic test.net-snmp.org sysUpTime.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77
Basic Example
In the example above, test.net-snmp.org is the host name of the agent to query, using version 1 of the SNMP protocol, and the community string "demopublic". The OID being requested is sysUpTime.0 from the MIB module SNMPv2-MIB. See snmptranslate for a discussion of the various ways to specify an OID.
Note that (unlike snmptranslate), snmpget will do random-lookup by default, so can work with a bare MIB object name (as shown), although the MODULE::name syntax or specifying the full OID are more reliable.
The same basic command can also be used to retrieve a single element from within a table:
% snmpget -v 1 -c demopublic test.net-snmp.org sysORDescr.1 SNMPv2-MIB::sysORDescr.1 = STRING: The Mib module for SNMPv2 entities
SNMP Versions
Both the original SNMPv1 and the later SNMPv2c use the clear-text "community string" as a de-facto password, to indicate whether a particular request should be authorised or not. The SNMPv2c equivalent of the above example would be almost identical:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586731977) 67 days, 21:48:39.77
SNMPv3 uses a significantly different athentication mechanism - see SNMPv3 Options for details.
The default version used depends on how the software was configured when it was first compiled. Typically, the Net-SNMP suite will use SNMPv3 by default, but it is safest to always specify the version explicitly.
Problems
A common mistake when using the snmpget command is to forget the index (or "instance subidentifier") of the data you're looking for. This is less of a danger when retrieving a value from within a table, where it is natural to include an index as part of the OID. But SNMP is consistent in requiring an instance for all MIB objects - even scalar objects, where there is only ever one value to retrieve. In this case, the instance subidentifier is always a simple .0 (zero), as shown in the examples above.
Omitting this results in an error:
% snmpget -v 1 -c demopublic test.net-snmp.org sysUpTime Error in packet Reason: (noSuchName) There is no such variable name in this MIB. This name doesn't exist: sysUpTime
Note that SNMPv2c gives a slightly more informative message:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime SNMPv2-MIB::sysUpTime = No Such Instance currently exists
This contrasts with the message displayed for an OID that the agent does not support at all:
% snmpget -v 2c -c demopublic test.net-snmp.org .1.3.6.1.2.1.1.99.0 SNMPv2-MIB::system.99.0 = No Such Object available on this agent at this OID
SNMPv1 uses noSuchName for both situations. SNMPv3 reports results in the same way as SNMPv2c.
Multiple Variables
All the examples so far have worked with a single value. But snmpget can also retrieve several variables in a single transaction:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 sysLocation.0 SNMPv2-MIB::sysUpTime.0 = Timeticks: (586903243) 67 days, 22:17:12.43 SNMPv2-MIB::sysLocation.0 = UCDavis
This works in the same way for both SNMPv1 and SNMPv2c. The difference between the two becomes apparent when one of the OIDs being requested is not valid. SNMPv2c (and SNMPv3) will display what information they can:
% snmpget -v 2c -c demopublic test.net-snmp.org sysUpTime.0 sysLocation SNMPv2-MIB::sysUpTime.0 = Timeticks: (586903243) 67 days, 22:17:12.43 SNMPv2-MIB::sysLocation = No Such Instance currently exists
while the equivalent SNMPv1 request will simply fail:
% snmpget -Cf -v 1 -c demopublic test.net-snmp.org sysUpTime.0 sysLocation Error in packet Reason: (noSuchName) There is no such variable name in this MIB. This name doesn't exist: sysLocation
(Note that the -Cf flag is needed to prevent snmpget from automatically correcting this problem, and retrying the request - thus defeating the point of this example!)
Tutorial Sections
About the SNMP Protocol
These tutorial links talk about SNMP generically and how the protocol itself works. They are good introductory reading material and the concepts are important to understand before diving into the later tutorials about Net-SNMP itself.
- How SNMP Works: About the protocol itself (GETs, GETNEXTs, etc)
- What data is in SNMP: All about SNMP Management Information Bases (MIBs)
- Securing SNMP: How to use the SNMP protocol securely
Net-SNMP Command Line Applications
These tutorial pages discuss the command line tools provided in the Net-SNMP suite of tools. Nearly all the example commands in these tutorials works if you try it yourself, as they're all examples that talk to our online Net-SNMP test agent. Given them a shot!
- snmptranslate: learning about the MIB tree.
- snmpget: retrieving data from a host.
- snmpgetnext: retrieving unknown indexed data.
- snmpwalk: retrieving lots of data at once!
- snmptable: displaying a table.
- snmpset: peforming write operations.
- snmpbulkget: communicates with a network entity using SNMP GETBULK request
- snmpbulkwalk: retrieve a sub-tree of management values using SNMP GETBULK requests.
- snmptrap: Sending and receiving traps, and acting upon them.
- Traps/informs with SNMPv3/USM: Sending and receiving SNMPv3/USM TRAPs and INFORMs
- Sending Traps/Informs via AgentX: Sending notifications from the command line through snmpd
- Common command line options:
- Writing mib2c config files
Application Configuration
All of our applications support configuration to allow you to customize how they behave.
Net-SNMP Daemons
Net-SNMP comes with two long-running daemons: a SNMP agent (snmpd) for responding to management requests and a notification receiver (snmptrapd) for receiving SNMP notifications.
- SNMP Agent (snmpd) Configuration
- SNMP Notification Receiver (snmptrapd)
- Agent Monitoring
Coding Tutorials
Net-SNMP comes with a highly flexible and extensible API. The API allows you to create your own commands, add extensions to the agent to support your own MIBs and perform specialized processing of notifications.
- Client / Manager Coding Tutorials
- Agent Coding Tutorials
- The Agent Architecture page might be worth reading before or after the agent coding tutorials, and describes how the Agent Helpers work under the hood.
- Writing a mib module to serve information described by an SNMP MIB, and how to compile it into the net-snmp snmpd agent.
- Writing a Dynamically Loadable Object that can be loaded into the SNMP agent.
- Writing a Subagent that can be run to attach to the snmpd master agent.
- Writing a perl plugin to extend the agent using the NetSNMP::agent module.
- Writing shell scripts to extend the agent
- Using mib2c to help write an agent code template for you
- Header files and autoconf
Debugging SNMP Applications and Agents
All our tools and applications have extensive debugging output. These tutorials talk about how the debugging system works and how you can add your own debugging statements to you code:
- Debugging output printed using the -D command line option
- Using -Ddump to display packet breakdowns
- Debugging using GDB
Operating System Specific Tutorials
- Building With Visual Studio 2005 Express
- Building Net-SNMP 64-bit with Visual C++ 2010 Express
- Net-Snmp on Ubuntu
- Net-SNMP and lm-sensors on Ubuntu 10.04
- Net-SNMP for windows: