summaryrefslogtreecommitdiff
path: root/lib/oxidized/model
diff options
context:
space:
mode:
Diffstat (limited to 'lib/oxidized/model')
-rw-r--r--lib/oxidized/model/ios.rb20
-rw-r--r--lib/oxidized/model/model.rb42
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|