SNMP Library for Ruby


This library implements SNMP (the Simple Network Management Protocol). It is implemented in pure Ruby, so there are no dependencies on external libraries like net-snmp. You can run this library anywhere that Ruby can run.

See for more info.

This release supports the following:

  • The GetRequest, GetNextRequest, GetBulkRequest, SetRequest, Response SNMPv1_Trap, SNMPv2_Trap, and Inform PDUs

  • All of the ASN.1 data types defined by SNMPv1 and SNMPv2c

  • Sending informs and v1 and v2 traps

  • Trap handling for informs and v1 and v2 traps

  • Symbolic OID values (ie. “ifTable” instead of “”) as parameters to the SNMP::Manager API

  • Includes symbol data files for all current IETF MIBs

  • Compatibility with both Ruby 1.8 and Ruby 1.9

See the SNMP::Manager, SNMP::TrapListener, and SNMP::MIB classes and the examples below for more details.


Changes for version 1.1.0:

  • Added MIB support to ObjectId and Varbind, so that to_s can return symbolic information

  • Added to_str method to ObjectId to return a numeric OID string (old to_s behavior)

  • TrapListener can now support multiple community strings

Changes for version 1.0.4:

  • New option handling and added lower-case versions of all options

  • Added SNMP::VERSION constant

  • Experimental support for IPv6

  • Removed support for installation with setup.rb

Changes for version 1.0.3:

  • Minor changes to Manager class. The :Transport option may now be an object or a class. Explicity call Timeout.timeout so that a timeout method may be defined in subclasses. Thanks to Eric Monti.

Changes for version 1.0.2:

  • Internal code changes to make this library compatible with both Ruby 1.8 and Ruby 1.9. Note that an ord() method is now added to the Fixnum class for Ruby 1.8. See the ber.rb file for details.

Changes for version 1.0.1:

  • Made the host configurable for the TrapListener. Previously defaulted to 'localhost'.

Changes for version 1.0.0:

  • Added to_s method to TimeTicks. Displays time in human-readable form instead of just a number. The to_i method can still be used to get the number of ticks.


You can use RubyGems [] to install the latest version of the SNMP library.

gem install snmp


This library has received limited testing:

  • The unit tests have been executed with both Ruby 1.8.7 and Ruby 1.9.2 on Mac OS X 10.7.

  • Basic interoperability testing has been done with the net-snmp tools.

I'm very interested in hearing about successes or failures on other platforms.

Send me an email at hallidave at


Get Request

Retrieve a system description.

require 'snmp' => 'localhost') do |manager|
    response = manager.get(["sysDescr.0", "sysName.0"])
    response.each_varbind do |vb|
        puts "#{}  #{vb.value.to_s}  #{vb.value.asn1_type}"

Set Request

Create a varbind for setting the system name.

require 'snmp'
include SNMP

manager = => 'localhost')
varbind ="","My System Name"))

Table Walk

Walk the ifTable.

require 'snmp'

ifTable_columns = ["ifIndex", "ifDescr", "ifInOctets", "ifOutOctets"] => 'localhost') do |manager|
    manager.walk(ifTable_columns) do |row|
        row.each { |vb| print "\t#{vb.value}" }

Get-Next Request

A more difficult way to walk the ifTable.

require 'snmp'
include SNMP => 'localhost') do |manager|
    ifTable ="")
    next_oid = ifTable
    while next_oid.subtree_of?(ifTable)
        response = manager.get_next(next_oid)
        varbind = response.varbind_list.first
        next_oid =
        puts varbind.to_s

Get-Bulk Request

Get interface description and admin status for 10 rows of the ifTable.

require 'snmp'
include SNMP

ifDescr_OID ="")
ifAdminStatus_OID ="")
MAX_ROWS = 10 => 'localhost') do |manager|
    response = manager.get_bulk(0, MAX_ROWS, [ifDescr_OID, ifAdminStatus_OID])
    list = response.varbind_list
    until list.empty?
        ifDescr = list.shift
        ifAdminStatus = list.shift
        puts "#{ifDescr.value}    #{ifAdminStatus.value}"

Trap Handling

Log traps to STDOUT.

require 'snmp'
require 'logger'

log =
m = do |manager|
    manager.on_trap_default do |trap| trap.inspect


This SNMP Library is Copyright © 2004-2011 by David R. Halliday. It is free software. Redistribution is permitted under the same terms and conditions as the standard Ruby distribution. See the COPYING file in the Ruby distribution for details.