Method: NETSNMP::Client#initialize

Defined in:
lib/netsnmp/client.rb

#initialize(version: nil, **options) {|client| ... } ⇒ Client

Returns a new instance of Client.

Examples:

Yielding a clinet

NETSNMP::Client.new(host: "241.232.22.12") do |client|
  puts client.get(oid: "1.3.6.1.2.1.1.5.0")
end

Parameters:

  • options (Hash)

    the options to needed to enable the SNMP client.

Options Hash (**options):

  • :version (String, Integer, nil)

    the version of the protocol (defaults to 3). also accepts common known declarations like :v3, “v2c”, etc

  • :retries (Integer)

    number of retries for each failed PDU (after which it raise timeout error. Defaults to RETRIES retries)

Yields:

  • (client)

    the instantiated client, after which it closes it for use.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/netsnmp/client.rb', line 26

def initialize(version: nil, **options)
  version = case version
            when Integer then version # assume the use know what he's doing
            when /v?1/ then 0
            when /v?2c?/ then 1
            when /v?3/ then 3
            else 3 # rubocop:disable Lint/DuplicateBranch
            end

  @retries = options.fetch(:retries, RETRIES).to_i
  @session ||= version == 3 ? V3Session.new(**options) : Session.new(version: version, **options)
  return unless block_given?

  begin
    yield self
  ensure
    close
  end
end