TUT:snmpget

From Net-SNMP Wiki
Revision as of 16:30, 9 March 2007 by Jifl (Talk | contribs) (spelling mistake)

Jump to: navigation, search

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 authentication 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.

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!

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.

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.

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:

Operating System Specific Tutorials