Module: Expect4r::Router::Vyatta::Modes

Included in:
V
Defined in:
lib/router/vyatta/modes.rb

Instance Method Summary collapse

Instance Method Details

#change_mode_to(mode) ⇒ Object



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

def change_mode_to(mode)
   unless connected?
  case mode
  when :exec    ;  to_exec
  when :config  ;  to_config
  end
end

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

Adds a Vyatta config mixin.

Example:

c.config %{
  edit protocols bgp 100
    set neighbor 192.168.129.1
    set neighbor 192.168.129.1 capability orf prefix-list receive
    set neighbor 192.168.129.1 ebgp-multihop 10
    set neighbor 192.168.129.1 remote-as 200
}


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/router/vyatta/modes.rb', line 18

def config(stmts=nil, arg={})
   unless connected?
  if stmts
    mode = in?
    to_config
    output = exp_send(stmts, arg)
    output << commit
    change_mode_to(mode)
    output.flatten
  else
    to_config
  end
end

#config?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/router/vyatta/modes.rb', line 65

def config?
  if logged_as_root?
     @_lp_1 =~ /\[edit\]/ ? true : false
  else
    @lp =~ /^.+# $/ ? true : false
  end
end

#exec(cmd = nil, *args) ⇒ Object

Adds a Vyatta exec mixin.

Example:

v.exec 'set cli screen-length 0'


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/router/vyatta/modes.rb', line 38

def exec(cmd=nil, *args)
   unless connected?
  if cmd.nil?
    to_exec
  else
    if config?
      exp_send("run #{cmd}", *args)
    elsif exec?
      exp_send cmd, *args
    else
      mode = _mode_?
      to_exec
      output = exp_send(cmd, *args)
      change_mode_to mode
      output
    end
  end
end

#exec?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/router/vyatta/modes.rb', line 57

def exec?
  if logged_as_root?
    ! config?
  else
    @lp =~ /\$ $/ ? true : false
  end
end

#in?(mode = :none) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
# File 'lib/router/vyatta/modes.rb', line 73

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

#to_configObject

Raises:

  • (RuntimeError)


83
84
85
86
87
88
89
# File 'lib/router/vyatta/modes.rb', line 83

def to_config
  return :config if config?
  to_exec
  putline 'configure', :debug=>1
  raise RuntimeError, "unable to get to config mode" unless config?
  :config
end

#to_execObject

Raises:

  • (RuntimeError)


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

def to_exec
  return :exec if exec?
  top if config?
  exit
  raise RuntimeError, "unable to get to exec mode" unless exec?
  :exec
end