Class: SNMP::Open::CommandReader
- Inherits:
-
Object
- Object
- SNMP::Open::CommandReader
- Defined in:
- lib/snmp/open/command_reader.rb
Overview
Open3-based data source that executes an snmp* command and captures the output
Instance Method Summary collapse
- #capture(cmd, oid, options = {}) ⇒ Object
-
#cli(command, id = nil, options = {}) ⇒ Object
Generate a CLI command string.
-
#initialize(options) ⇒ CommandReader
constructor
options
accepts options dealing with making connections to the host, including all of the options listed in theOptions::MAP
constant hash.
Constructor Details
#initialize(options) ⇒ CommandReader
options
accepts options dealing with making connections to the host, including all of the options listed in the Options::MAP
constant hash. Other options can be given as strings (or any object with a suitable to_s
method), e.g., these are equivalent:
SNMP::Open.new(host: hostname, timeout: 3, '-m' => miblist)
SNMP::Open.new(hostname => nil, '-t' => '3', '-m' => miblist)
17 18 19 20 21 22 23 24 25 |
# File 'lib/snmp/open/command_reader.rb', line 17 def initialize() @env = .delete(:env) host = .delete(:host) || (raise ArgumentError, 'Host expected but not given') opts = Options::REQUIRED_BY_PARSER .merge(()) .merge(host => nil) @command_options, @host_options = (opts) end |
Instance Method Details
#capture(cmd, oid, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/snmp/open/command_reader.rb', line 27 def capture(cmd, oid, = {}) out, err = if @env Open3.capture3(@env, cli(cmd, oid, )) else Open3.capture3(cli(cmd, oid, )) end raise_capture_errors(err) out end |
#cli(command, id = nil, options = {}) ⇒ Object
Generate a CLI command string
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/snmp/open/command_reader.rb', line 38 def cli(command, id = nil, = {}) command = normalize_command(command) [ command, *.map { |k, v| "#{k}#{v}" }, *(id), *@host_options.map { |k, v| "#{k}#{v}" }, *@command_options.fetch(command, {}).map { |k, v| "#{k}#{v}" }, *id ].join(' ') end |