diff options
| author | Saku Ytti <saku@ytti.fi> | 2013-05-01 13:45:02 +0300 | 
|---|---|---|
| committer | Saku Ytti <saku@ytti.fi> | 2013-05-01 14:06:44 +0300 | 
| commit | c252a589b6b0274a17bfe40db3fd57ebeea3beb8 (patch) | |
| tree | 0115a10db0f37506e72aaf4a873aeea93734f6ab /lib/oxidized/model | |
| parent | 387b725fcd248d052cfe68da97f03192959ad6a4 (diff) | |
Add Model#expect, support block at post/pre config
Now we can deal with pager and additional PW prompts, such as 'enable'
Examples in IOS model how to use.
The Telnet implementation is particularly fugly, I just need one line in
'waitfor' to handle pager while waiting for prompt, but couldn't figure
out clean way to do it, so needed to rewrit whole Telnet#waitfor just to
add that line.
Diffstat (limited to 'lib/oxidized/model')
| -rw-r--r-- | lib/oxidized/model/ios.rb | 20 | ||||
| -rw-r--r-- | lib/oxidized/model/model.rb | 42 | 
2 files changed, 54 insertions, 8 deletions
| diff --git a/lib/oxidized/model/ios.rb b/lib/oxidized/model/ios.rb index c3027e2..7da5ed2 100644 --- a/lib/oxidized/model/ios.rb +++ b/lib/oxidized/model/ios.rb @@ -2,7 +2,22 @@ class IOS < Oxidized::Model    comment  '! ' +  # example how to handle pager +  #expect /^\s--More--\s+.*$/ do |data, re| +  #  send ' ' +  #  data.sub re, '' +  #end + +  # non-preferred way to handle additional PW prompt +  #expect /^[\w.]+>$/ do |data| +  #  send "enable\n" +  #  send CFG.passwords[:enable] + "\n" +  #  data +  #end +    cmd :all do |cfg| +    #cfg.gsub! /\cH+\s{8}/, ''         # example how to handle pager +    #cfg.gsub! /\cH+/, ''              # example how to handle pager      cfg.each_line.to_a[1..-3].join    end @@ -24,6 +39,11 @@ class IOS < Oxidized::Model    cfg :telnet, :ssh do      post_login 'terminal length 0'      post_login 'terminal width 0' +    # preferred way to handle additional passwords +    #post_login do +    #  send "enable\n" +    #  send CFG.passwords[:enable] + "\n" +    #end      pre_logout 'exit'    end diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index 8f7b14f..2a6cbc4 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -2,22 +2,24 @@ module Oxidized    class Model      class << self        def inherited klass -        klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] } -        klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] } +        klass.instance_variable_set '@cmd',    Hash.new { |h,k| h[k] = [] } +        klass.instance_variable_set '@cfg',    Hash.new { |h,k| h[k] = [] } +        klass.instance_variable_set '@expect', [] +        klass.const_set :CFG, CFG          Oxidized.mgr.loader = { :class => klass }        end        def comment _comment='# '          return @comment if @comment          @comment = block_given? ? yield : _comment        end +      def prompt _prompt=nil +        @prompt or @prompt = _prompt +      end        def cfg *methods, &block          [methods].flatten.each do |method|            @cfg[method.to_s] << block          end        end -      def prompt _prompt=nil -        @prompt or @prompt = _prompt -      end        def cfgs          @cfg        end @@ -31,8 +33,11 @@ module Oxidized        def cmds          @cmd        end -      def post_login &block -        @post_login or @post_login = block +      def expect re, &block +        @expect << [re, block] +      end +      def expects +        @expect        end      end @@ -48,7 +53,15 @@ module Oxidized        out      end -    def  cfg +    def send data +      @input.send data +    end + +    def expect re, &block +      self.class.expect re, &block +    end + +    def cfg        self.class.cfgs      end @@ -56,6 +69,19 @@ module Oxidized        self.class.prompt      end +    def expects data +      self.class.expects.each do |re, cb| +        if data.match re +          if cb.arity == 2 +            data = instance_exec [data, re], &cb +          else +            data = instance_exec data, &cb +          end +        end +      end +      data +    end +      def get        data = ''        self.class.cmds[:cmd].each do |command, block| | 
