Class: Oxidized::Model

Inherits:
Object
  • Object
show all
Includes:
Config::Vars
Defined in:
lib/oxidized/model/model.rb,
lib/oxidized/model/outputs.rb

Defined Under Namespace

Classes: Outputs

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::Vars

#vars

Class Attribute Details

.procsHash (readonly)

Returns hash proc procs :pre+:post to be prepended/postfixed to output.

Returns:

  • (Hash)

    hash proc procs :pre+:post to be prepended/postfixed to output

Author:

Since:

  • 0.0.39



99
100
101
# File 'lib/oxidized/model/model.rb', line 99

def procs
  @procs
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



118
119
120
# File 'lib/oxidized/model/model.rb', line 118

def input
  @input
end

#nodeObject

Returns the value of attribute node.



118
119
120
# File 'lib/oxidized/model/model.rb', line 118

def node
  @node
end

Class Method Details

.cfg(*methods, **args, &block) ⇒ Object



43
44
45
46
47
# File 'lib/oxidized/model/model.rb', line 43

def cfg(*methods, **args, &block)
  [methods].flatten.each do |method|
    process_args_block(@cfg[method.to_s], args, block)
  end
end

.cfgsObject



49
50
51
# File 'lib/oxidized/model/model.rb', line 49

def cfgs
  @cfg
end

.cmd(cmd_arg = nil, **args, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/oxidized/model/model.rb', line 53

def cmd(cmd_arg = nil, **args, &block)
  if cmd_arg.instance_of?(Symbol)
    process_args_block(@cmd[cmd_arg], args, block)
  else
    process_args_block(@cmd[:cmd], args, [cmd_arg, block])
  end
  Oxidized.logger.debug "lib/oxidized/model/model.rb Added #{cmd_arg} to the commands list"
end

.cmdsObject



62
63
64
# File 'lib/oxidized/model/model.rb', line 62

def cmds
  @cmd
end

.comment(str = "# ") ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/oxidized/model/model.rb', line 29

def comment(str = "# ")
  @comment = if block_given?
               yield
             elsif not @comment
               str
             else
               @comment
             end
end

.expect(regex, **args, &block) ⇒ Object



66
67
68
# File 'lib/oxidized/model/model.rb', line 66

def expect(regex, **args, &block)
  process_args_block(@expect, args, [regex, block])
end

.expectsObject



70
71
72
# File 'lib/oxidized/model/model.rb', line 70

def expects
  @expect
end

.inherited(klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/oxidized/model/model.rb', line 11

def inherited(klass)
  super
  if klass.superclass == Oxidized::Model
    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 '@procs',   (Hash.new { |h, k| h[k] = [] })
    klass.instance_variable_set '@expect',  []
    klass.instance_variable_set '@comment', nil
    klass.instance_variable_set '@prompt',  nil
  else # we're subclassing some existing model, take its variables
    instance_variables.each do |var|
      iv = instance_variable_get(var)
      klass.instance_variable_set var, iv.dup
      @cmd[:cmd] = iv[:cmd].dup if var.to_s == "@cmd"
    end
  end
end

.post(**args) { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, adding the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



92
93
94
# File 'lib/oxidized/model/model.rb', line 92

def post(**args, &block)
  process_args_block(@procs[:post], args, block)
end

.pre(**args) { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, prepending the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



81
82
83
# File 'lib/oxidized/model/model.rb', line 81

def pre(**args, &block)
  process_args_block(@procs[:pre], args, block)
end

.prompt(regex = nil) ⇒ Object



39
40
41
# File 'lib/oxidized/model/model.rb', line 39

def prompt(regex = nil)
  @prompt = regex || @prompt
end

Instance Method Details

#cfgObject



150
151
152
# File 'lib/oxidized/model/model.rb', line 150

def cfg
  self.class.cfgs
end

#cmd(string, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/oxidized/model/model.rb', line 120

def cmd(string, &block)
  Oxidized.logger.debug "lib/oxidized/model/model.rb Executing #{string}"
  out = @input.cmd(string)
  return false unless out

  out = out.b unless Oxidized.config.input.utf8_encoded?
  self.class.cmds[:all].each do |all_block|
    out = instance_exec out, string, &all_block
  end
  if vars :remove_secret
    self.class.cmds[:secret].each do |all_block|
      out = instance_exec out, string, &all_block
    end
  end
  out = instance_exec out, &block if block
  process_cmd_output out, string
end

#comment(str) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/oxidized/model/model.rb', line 186

def comment(str)
  data = ''
  str.each_line do |line|
    data << self.class.comment << line
  end
  data
end

#expectObject



146
147
148
# File 'lib/oxidized/model/model.rb', line 146

def expect(...)
  self.class.expect(...)
end

#expects(data) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/oxidized/model/model.rb', line 158

def expects(data)
  self.class.expects.each do |re, cb|
    if data.match re
      data = cb.arity == 2 ? instance_exec([data, re], &cb) : instance_exec(data, &cb)
    end
  end
  data
end

#getObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/oxidized/model/model.rb', line 167

def get
  Oxidized.logger.debug 'lib/oxidized/model/model.rb Collecting commands\' outputs'
  outputs = Outputs.new
  procs = self.class.procs
  self.class.cmds[:cmd].each do |command, block|
    out = cmd command, &block
    return false unless out

    outputs << out
  end
  procs[:pre].each do |pre_proc|
    outputs.unshift process_cmd_output(instance_eval(&pre_proc), '')
  end
  procs[:post].each do |post_proc|
    outputs << process_cmd_output(instance_eval(&post_proc), '')
  end
  outputs
end

#outputObject



138
139
140
# File 'lib/oxidized/model/model.rb', line 138

def output
  @input.output
end

#promptObject



154
155
156
# File 'lib/oxidized/model/model.rb', line 154

def prompt
  self.class.prompt
end

#screenscrapeObject



212
213
214
# File 'lib/oxidized/model/model.rb', line 212

def screenscrape
  @input.class.to_s.match(/Telnet/) || vars(:ssh_no_exec)
end

#send(data) ⇒ Object



142
143
144
# File 'lib/oxidized/model/model.rb', line 142

def send(data)
  @input.send data
end

#xmlcomment(str) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/oxidized/model/model.rb', line 194

def xmlcomment(str)
  # XML Comments start with <!-- and end with -->
  #
  # Because it's illegal for the first or last characters of a comment
  # to be a -, i.e. <!--- or ---> are illegal, and also to improve
  # readability, we add extra spaces after and before the beginning
  # and end of comment markers.
  #
  # Also, XML Comments must not contain --. So we put a space between
  # any double hyphens, by replacing any - that is followed by another -
  # with '- '
  data = ''
  str.each_line do |_line|
    data << '<!-- ' << str.gsub(/-(?=-)/, '- ').chomp << " -->\n"
  end
  data
end