http://www.net-snmp.org/wiki/index.php/TUT:snmpset
The
SET request is used to modify information on the target agent - updatingthe configuration of that agent, or controlling the behaviour of the remote system. This protocol operation can be sent via the
snmpset command line tool.
Basic Example
The syntax of the snmpset
command is similar to that of the
command, and most of the
snmpget
snmpget tutorial applies here too.The main difference is in specifying the information to work with.Instead of a single OID, the
snmpset
command requires theOID to update, the data type of this object, and the new value to apply:
snmpset -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 s "hi there!"
The effect of this command can usually be seen by retrieving the value of an object,both before and after the SET request:
$ snmpget -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 UCD-DEMO-MIB::ucdDemoPublicString.0 = "hi there!" $ snmpset -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 s "Hello, world!" UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello, world!" $ snmpget -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello, world!"
Note that the values returned following the SET request will always be thesame as those provided. This is
normally the same as that returned bya subsequent GET request (as shown above), but not necessarily:
$ snmpget test.net-snmp.org snmpSetSerialNo.0 SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123456 $ snmpset test.net-snmp.org snmpSetSerialNo.0 i 123456 SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123456 $ snmpget test.net-snmp.org snmpSetSerialNo.0 SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123457
Data Types
The list of valid datatypes can be found at the end of the snmpset help output:
$ snmpset -h |& tail -4 type - one of i, u, t, a, o, s, x, d, n i: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS o: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING U: unsigned int64, I: signed int64, F: float, D: double
Note that the last four types are only valid when talking to theNet-SNMP agent. They are not part of the official SNMP specification.
Assuming that the MIB file is loaded, then it's also possible tospecify the type as "=", and the
snmpset
command willsupply the appropriate type from the MIB file:
$ snmpset test.net-snmp.org ucdDemoPublicString.0 = "Hello clouds" UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello clouds"
This doesn't work if the MIB file isn't loaded, of course - but thenreferring to the MIB object by name wouldn't either!
Multiple Values
As with the snmpget
command, it also is possible to SET severalnew values in the one request. Simply specify the list of (OID,type,value)triples on the command line:
$ snmpset test.net-snmp.org ucdDemoPublicString.0 s "Hello sky" snmpSetSerialNo.0 i 123457 UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello sky" SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123457
If one of these assigments is invalid, then the request will be rejected without applyingany of the new values - regardless of the order they appear in the list.This is quite useful for the administrator wanting to manage their systems,but can be
something of a headache for the poor schmuck landed with thetask of implementing the SET handling within the agent.
Failed Requests
If the MIB file has been loaded, and the supplied value is invalid according tothe MIB definitions (e.g. the wrong type, or outside the valid range for thatobject), then <command>snmpset</command> will display a failure messagewithout ever sending the request
to the target agent:
$ snmpset test.net-snmp.org snmpSetSerialNo.0 s "as any fule kno" snmpSetSerialNo.0: Bad variable type (Type of attribute is INTEGER, not OCTET STRING)
If the MIB file is not available, or the value matches the syntax from theMIB definition, then the request will be sent to the target agent whichmay then reject the request:
$ snmpset test.net-snmp.org snmpSetSerialNo.0 i 9999 Error in packet. Reason: (badValue) The value given has the wrong type or length Failed object: SNMPv2-MIB::snmpSetSerialNo.0
The same effect can be seen by suppressing the local validation:
$ snmpset test.net-snmp.org -Ir snmpSetSerialNo.0 s "How To Be Topp" Error in packet. Reason: (badValue) The value given has the wrong type or length Failed object: SNMPv2-MIB::snmpSetSerialNo.0
SNMPv1 reports such problems using a single error report (badValue
)as shown above. SNMPv2c is a little more informative:
$ snmpset -v 2c test.net-snmp.org -Ir snmpSetSerialNo.0 s "uterly wet"
Error in packet.
Reason: (wrongType) The set datatype does not match the data type the agent expects
Failed object: SNMPv2-MIB::snmpSetSerialNo.0
<tasks>[ ] Actually returns wrongValue
</tasks>
$ snmpset -v 2c test.net-snmp.org snmpSetSerialNo.0 i 9999
Error in packet.
Reason: (wrongValue) The set value is illegal or unsupported in some way
Failed object: SNMPv2-MIB::snmpSetSerialNo.0
Similarly, if you don't have permission to write to an object, the error reportedwill be different depending on the version of SNMP used:
$ snmpset -v 1 -c rocommunity test.net-snmp.org snmpSetSerialNo.0 i 123457
Error in packet.
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: SNMPv2-MIB::snmpSetSerialNo.0
$ snmpset -v 2c -c rocommunity test.net-snmp.org snmpSetSerialNo.0 i 123457
Error in packet.
Reason: noAccess
Failed object: SNMPv2-MIB::snmpSetSerialNo.0
<tasks>[ ] Should this return notWritable
?</tasks>
SNMPv3 uses the same (improved) error codes as SNMPv2c,as well as providing much better security - which is potentiallyquite important when it comes to SET requests!