Class: NETSNMP::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/netsnmp/session.rb

Overview

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

Direct Known Subclasses

V3Session

Defined Under Namespace

Classes: Transport

Constant Summary collapse

TIMEOUT =
2

Instance Method Summary collapse

Constructor Details

#initialize(version: 1, community: "public", **options) ⇒ Session

Returns a new instance of Session.

Parameters:

  • opts (Hash)

    the options set


12
13
14
15
16
# File 'lib/netsnmp/session.rb', line 12

def initialize(version: 1, community: "public", **options)
  @version   = version
  @community = community
  validate(**options)
end

Instance Method Details

#build_pdu(type, *vars) ⇒ NETSNMP::PDU

Returns a pdu.

Parameters:

  • type (Symbol)

    the type of PDU (:get, :set, :getnext)

  • vars (Array<Hashes>)

    collection of options to generate varbinds (see NETSMP::Varbind.new for all the possible options)

Returns:


30
31
32
# File 'lib/netsnmp/session.rb', line 30

def build_pdu(type, *vars)
  PDU.build(type, headers: [@version, @community], varbinds: vars)
end

#closeObject

Closes the session


19
20
21
22
23
# 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) ⇒ NETSNMP::PDU

send a pdu, receives a pdu

Parameters:

Returns:


40
41
42
43
44
45
46
47
48
49
50
51
# 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