lib/oxidized/script/script.rb in oxidized-script-0.0.4 vs lib/oxidized/script/script.rb in oxidized-script-0.0.6

- old
+ new

@@ -1,15 +1,19 @@ #!/usr/bin/env ruby module Oxidized require 'oxidized' + require_relative 'command' class Script class ScriptError < OxidizedError; end - class NoConnection < ScriptError; end class NoNode < ScriptError; end class InvalidOption < ScriptError; end + class NoConnection < ScriptError + attr_accessor :node_error + end + # @param [String] command command to be sent # @return [String] output for command def cmd command out = '' out += "## OXS - #{command}\n" if @verbose @@ -32,10 +36,11 @@ # @option opts [Fixnum] :timeout oxidized timeout # @option opts [String] :username username for login # @option opts [String] :passsword password for login # @option opts [String] :enable enable password to use # @option opts [String] :community community to use for discovery + # @option opts [String] :protocols protocols to use to connect, default "ssh ,telnet" # @option opts [boolean] :verbose extra output, e.g. show command given in output # @yieldreturn [self] if called in block, returns self and disconnnects session after exiting block # @return [void] def initialize opts, &block host = opts.delete :host @@ -44,10 +49,11 @@ username = opts.delete :username password = opts.delete :password enable = opts.delete :enable community = opts.delete :community @verbose = opts.delete :verbose + CFG.input.default = opts.delete :protocols if opts[:protocols] raise InvalidOption, "#{opts} not recognized" unless opts.empty? Oxidized.mgr = Manager.new @node = if model Node.new(:name=>host, :model=>model) else @@ -76,19 +82,23 @@ disconnect end end def connect + node_error = {} @node.input.each do |input| begin @node.model.input = input.new @node.model.input.connect @node break - rescue + rescue => error + node_error[input] = error end end @input = @node.model.input - raise NoConnection, 'unable to connect' unless @input.connected? + err = NoConnection.new + err.node_error = node_error + raise err, 'unable to connect' unless @input.connected? @input.connect_cli end end end