From c252a589b6b0274a17bfe40db3fd57ebeea3beb8 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Wed, 1 May 2013 13:45:02 +0300 Subject: =?UTF-8?q?Add=20Model#expect,=20support=20block=20at=C2=A0post/pr?= =?UTF-8?q?e=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/oxidized/model/ios.rb | 20 ++++++++++++++++++++ lib/oxidized/model/model.rb | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 8 deletions(-) (limited to 'lib/oxidized/model') 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| -- cgit v1.2.1