class NETSNMP::Session

Let's just remind that there is no session in snmp, this is just an abstraction.

Constants

TIMEOUT

Public Class Methods

new(version: 1, community: "public", **options) click to toggle source

@param [Hash] opts the options set

# File lib/netsnmp/session.rb, line 12
def initialize(version: 1, community: "public", **options)
  @version   = version
  @community = community
  validate(**options)
end

Public Instance Methods

build_pdu(type, *vars) click to toggle source

@param [Symbol] type the type of PDU (:get, :set, :getnext) @param [Array<Hashes>] vars collection of options to generate varbinds (see {NETSMP::Varbind.new} for all the possible options)

@return [NETSNMP::PDU] a pdu

# File lib/netsnmp/session.rb, line 30
def build_pdu(type, *vars)
  PDU.build(type, headers: [@version, @community], varbinds: vars)
end
close() click to toggle source

Closes the session

# File lib/netsnmp/session.rb, line 19
def close
  # if the transport came as an argument,
  # then let the outer realm care for its lifecycle
  @transport.close unless @proxy
end
send(pdu) click to toggle source

send a pdu, receives a pdu

@param [NETSNMP::PDU, to_der] an encodable request pdu

@return [NETSNMP::PDU] the response pdu

# File lib/netsnmp/session.rb, line 40
def send(pdu)
  log { "sending request..." }
  log(level: 2) { pdu.to_hex }
  encoded_request = pdu.to_der
  log { Hexdump.dump(encoded_request) }
  encoded_response = @transport.send(encoded_request)
  log { "received response" }
  log { Hexdump.dump(encoded_response) }
  response_pdu = PDU.decode(encoded_response)
  log(level: 2) { response_pdu.to_hex }
  response_pdu
end

Private Instance Methods

validate(host: nil, port: 161, proxy: nil, timeout: TIMEOUT, **) click to toggle source
# File lib/netsnmp/session.rb, line 55
def validate(host: nil, port: 161, proxy: nil, timeout: TIMEOUT, **)
  if proxy
    @proxy = true
    @transport = proxy
  else
    raise "you must provide an hostname/ip under :host" unless host
    @transport = Transport.new(host, port.to_i, timeout: timeout)
  end
  @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
               raise "unsupported snmp version (#{@version})"
             end
end