Module: Expect4r::Router::Ios::Modes

Included in:
Ios
Defined in:
lib/router/cisco/ios/modes.rb

Instance Method Summary collapse

Instance Method Details

#_mode_?Boolean



54
55
56
57
58
59
60
61
62
# File 'lib/router/cisco/ios/modes.rb', line 54

def _mode_?
  if user? 
    :user
  elsif config?
    :config
  elsif exec?
    :exec
  end
end

#config(config = nil, arg = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/router/cisco/ios/modes.rb', line 22

def config(config=nil, arg={})
   unless connected?
  if config
    mode = in?
    change_mode_to :config
    output = exp_send(config, arg)
    change_mode_to mode
    output
  else
    change_mode_to :config
  end
end

#config?Boolean



64
65
66
# File 'lib/router/cisco/ios/modes.rb', line 64

def config?
  @lp =~ /\(config(|.+)\)/
end

#exec(cmd = nil, arg = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/router/cisco/ios/modes.rb', line 35

def exec(cmd=nil, arg={})
   unless connected?
  if cmd.nil?
    change_mode_to :exec
  else
    if exec?
      output = exp_send(cmd, arg)
    elsif config?
      output = exp_send("do #{cmd}", arg)
    else
      mode = in?
      change_mode_to :exec
      output = exp_send(cmd, arg)
      change_mode_to mode
      output
    end
  end
end

#exec?Boolean

returns true if router is in :exec (enabled) mode, false otherwise.



71
72
73
# File 'lib/router/cisco/ios/modes.rb', line 71

def exec?
  ! user? and ! config?
end

#in?(mode = :none) ⇒ Boolean

Adds a in? mixin.

Returns the mode the router is in: :user, :exec, :config



10
11
12
13
14
15
16
17
18
19
# File 'lib/router/cisco/ios/modes.rb', line 10

def in?(mode=:none)
   unless connected?
  case mode
  when :exec    ; exec?
  when :user    ; user?
  when :config  ; config?
  else
    _mode_?
  end
end

#to_configObject

Raises:

  • (RuntimeError)


82
83
84
85
86
87
88
# File 'lib/router/cisco/ios/modes.rb', line 82

def to_config
  return :config if config?
  to_exec
  putline 'configure terminal' if exec?
  raise RuntimeError, "unable to get to config mode" unless config?
  :config
end

#to_execObject

Raises:

  • (RuntimeError)


90
91
92
93
94
95
96
97
# File 'lib/router/cisco/ios/modes.rb', line 90

def to_exec
  return :exec if exec?
  if config?
    1.upto(config_lvl?) { putline 'exit'}
  end
  raise RuntimeError, "unable to get to exec mode" unless exec?
  :exec
end

#to_userObject

Raises:

  • (RuntimeError)


99
100
101
102
103
104
# File 'lib/router/cisco/ios/modes.rb', line 99

def to_user
  return if user?
  putline "exit"
  raise RuntimeError, "unable to get to user mode" unless exec?
  :user
end

#user?Boolean

returns true if router is in :user mode, false otherwise.



78
79
80
# File 'lib/router/cisco/ios/modes.rb', line 78

def user?
  @lp.split.last =~ /.+>\s*$/
end