Sha256: 6b57e96f1d66a69f8aae6091dda575a1581b77f1cb7995f258fc639e6cd66bda

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module Oxidized
  require_relative 'script'
  require 'slop'
  class Script
    class CLI
      class CLIError < ScriptError; end
      class NothingToDo < ScriptError; end

      def run
        connect
        if @opts[:commands]
          run_file @opts[:commands]
        elsif @cmd
          @oxs.cmd @cmd
        end
      end

      private

      def initialize
        args, @opts = opts_parse
        CFG.debug = true if @opts[:debug]
        @host = args.shift
        @cmd  = args.shift if args
        @oxs  = nil
        raise NothingToDo, 'no host given' if not @host
        raise NothingToDo, 'nothing to do, give command or -x' if not @cmd and not @opts[:commands]
      end

      def opts_parse
        slop = Slop.parse(:help=>true) do
          banner 'Usage: oxs [options] hostname [command]'
          on 'm=', '--model',    'host model (ios, junos, etc), otherwise discovered from Oxidized source'
          on 'x=', '--commands', 'commands file to be sent'
          on 'u=', '--username', 'username to use'
          on 'p=', '--password', 'password to use'
          on 't=', '--timeout',  'timeout value to use'
          on 'e=', '--enable',   'enable password to use'
          on 'd',  '--debug',    'turn on debugging'
        end
        [slop.parse!, slop]
      end

      def connect
        opts = {}
        opts[:host]     = @host
        opts[:model]    = @opts[:model] if @opts[:model]
        opts[:username] = @opts[:username] if @opts[:username]
        opts[:password] = @opts[:password] if @opts[:password]
        opts[:timeout]  = @opts[:timeout].to_i if @opts[:timeout]
        opts[:enable]   = @opts[:enable] if @opts[:enable]
        @oxs = Script.new opts
      end

      def run_file file
        out = ''
        file = file == '-' ? $stdin : File.read(file)
        file.each_line do |line|
          line.sub!(/\\n/, "\n") # tread escaped newline as newline
          out += @oxs.cmd line
        end
        out
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oxidized-script-0.0.1 lib/oxidized/script/cli.rb