Class: PacketGen::Header::SNMP

Inherits:
ASN1Base
  • Object
show all
Defined in:
lib/packetgen/header/snmp.rb

Overview

Simple Network Management Protocol (SNMP)

See github.com/sdaubert/packetgen/wiki/SNMP

Author:

  • Sylvain Daubert

Since:

  • 2.0.0

Defined Under Namespace

Classes: Bulk, GetNextRequest, GetRequest, GetResponse, InformRequest, PDUs, Report, SetRequest, Trapv1, Trapv2, VarBind, VariableBindings

Constant Summary collapse

UDP_PORT1 =

Agents listen to this port

Since:

  • 2.0.0

161
UDP_PORT2 =

Configuration sinks listen to this port

Since:

  • 2.0.0

162
PDU_GET =

Since:

  • 2.0.0

0
PDU_NEXT =

Since:

  • 2.0.0

1
PDU_RESPONSE =

Since:

  • 2.0.0

2
PDU_SET =

Since:

  • 2.0.0

3
PDU_TRAPv1 =

Since:

  • 2.0.0

4
PDU_BULK =

Since:

  • 2.0.0

5
PDU_INFORM =

Since:

  • 2.0.0

6
PDU_TRAPv2 =

Since:

  • 2.0.0

7
PDU_REPORT =

Since:

  • 2.0.0

8
ERRORS =

Since:

  • 2.0.0

{
  'no_error'              => 0,
  'too_big'               => 1,
  'no_such_name'          => 2,
  'bad_value'             => 3,
  'read_only'             => 4,
  'generic_error'         => 5,
  'no_access'             => 6,
  'wrong_type'            => 7,
  'wrong_length'          => 8,
  'wrong_encoding'        => 9,
  'wrong_value'           => 10,
  'no_creation'           => 11,
  'inconsistent_value'    => 12,
  'ressource_unavailable' => 13,
  'commit_failed'         => 14,
  'undo_failed'           => 15,
  'authorization_error'   => 16,
  'not_writable'          => 17,
  'inconsistent_name'     => 18
}.freeze

Instance Method Summary collapse

Methods inherited from ASN1Base

define_attributes, #read

Methods included from PacketGen::Headerable

included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read

Constructor Details

#initialize(options = {}) ⇒ SNMP

Returns a new instance of SNMP.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :version (Integer, String)
  • :community (String)
  • :chosen_pdu (Integer)

    Set PDU type

  • :pdu (Hash)

    Set PDU content

Since:

  • 2.0.0



258
259
260
261
262
263
264
# File 'lib/packetgen/header/snmp.rb', line 258

def initialize(options={})
  super
  data.chosen = options[:chosen_pdu] if options[:chosen_pdu]
  return unless options[:pdu]

  data.root.value[data.chosen] = data.root.chosen_value.class.new(options[:pdu])
end

Instance Method Details

#added_to_packet(packet) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This method is used internally by PacketGen and should not be directly called

This method returns an undefined value.

Parameters:

Since:

  • 2.7.0 Set UDP sport according to bindings, only if sport is 0. Needed by new bind API.



308
309
310
311
312
313
# File 'lib/packetgen/header/snmp.rb', line 308

def added_to_packet(packet)
  return unless packet.is? 'UDP'
  return unless packet.udp.sport.zero?

  packet.udp.sport = packet.udp.dport
end

#dataGetRequest

accessor to data payload

Returns:

Since:

  • 2.0.0



268
269
270
# File 'lib/packetgen/header/snmp.rb', line 268

def data
  @elements[:data]
end

#inspectObject

Since:

  • 2.0.0



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/packetgen/header/snmp.rb', line 282

def inspect
  str = super
  str << Inspect.shift_level
  if self[:data].chosen.nil?
    str << Inspect::FMT_ATTR % [self[:data].type, :data, '']
  else
    data = self[:data]
    str << Inspect::FMT_ATTR % [data.type, :data, data.chosen_value.type]
    str << Inspect.dashed_line('ASN.1 content')
    str << data.chosen_value.inspect(1)
    begin
      str << Inspect.inspect_body(self[:message].to_der, 'ASN.1 DER')
    rescue StandardError => ex
      raise unless ex.message =~ /TAG.*not handled/
    end
    str
  end
end

#pduGetRequest, ...

shortcut to PDU

Returns:

Since:

  • 2.0.0



274
275
276
277
278
279
280
# File 'lib/packetgen/header/snmp.rb', line 274

def pdu
  if data.chosen.nil?
    nil
  else
    data.root.chosen_value
  end
end