Class: Rtprov::Session
- Inherits:
-
Object
- Object
- Rtprov::Session
- Defined in:
- lib/rtprov/session.rb
Instance Attribute Summary collapse
-
#prompt_pattern ⇒ Object
readonly
Returns the value of attribute prompt_pattern.
-
#prompt_prefix ⇒ Object
readonly
Returns the value of attribute prompt_prefix.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Class Method Summary collapse
Instance Method Summary collapse
- #as_administrator(&block) ⇒ Object
- #exec(cmd) ⇒ Object
- #exec_with_passwords(cmd) ⇒ Object
-
#initialize(router, reader, writer, prompt_prefix, prompt_suffix) ⇒ Session
constructor
A new instance of Session.
Constructor Details
#initialize(router, reader, writer, prompt_prefix, prompt_suffix) ⇒ Session
Returns a new instance of Session.
33 34 35 36 37 38 39 |
# File 'lib/rtprov/session.rb', line 33 def initialize(router, reader, writer, prompt_prefix, prompt_suffix) @router = router @reader = reader @writer = writer @prompt_prefix = prompt_prefix.dup.freeze @prompt_pattern = Regexp.compile("^" + Regexp.escape(prompt_prefix) + "[a-z1-9]*" + prompt_suffix + " ").freeze end |
Instance Attribute Details
#prompt_pattern ⇒ Object (readonly)
Returns the value of attribute prompt_pattern.
7 8 9 |
# File 'lib/rtprov/session.rb', line 7 def prompt_pattern @prompt_pattern end |
#prompt_prefix ⇒ Object (readonly)
Returns the value of attribute prompt_prefix.
7 8 9 |
# File 'lib/rtprov/session.rb', line 7 def prompt_prefix @prompt_prefix end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
7 8 9 |
# File 'lib/rtprov/session.rb', line 7 def reader @reader end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
7 8 9 |
# File 'lib/rtprov/session.rb', line 7 def router @router end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
7 8 9 |
# File 'lib/rtprov/session.rb', line 7 def writer @writer end |
Class Method Details
.start(router, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rtprov/session.rb', line 9 def self.start(router, &block) cmd = [ "ssh", "#{router.user}@#{router.host}", ].shelljoin PTY.getpty(cmd) do |r, w, _pid| w.sync = true r.expect(/password/) w.puts router.password prompt_prefix = r.expect(/^(.*)> /)[1] session = new(router, r, w, prompt_prefix, ">") session.exec("console character en.ascii") session.exec("console lines infinity") # disable pager session.exec("console columns 200") session.as_administrator(&block) w.puts "exit" end end |
Instance Method Details
#as_administrator(&block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rtprov/session.rb', line 71 def as_administrator(&block) writer.puts "administrator" reader.expect(/Password: /) writer.puts router.administrator_password reader.expect(/^.*# /) begin # set new prompt because default administrator prompt "# " matches config file comment etc. session = self.class.new(router, reader, writer, "RTPROV", "#") session.exec "console prompt RTPROV" block.call(session) ensure writer.puts "console prompt '#{prompt_prefix}'" reader.expect(/^.*# /) end writer.puts "exit" reader.expect "Save new configuration ? (Y/N)" writer.puts "Y" reader.expect(prompt_pattern) end |
#exec(cmd) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rtprov/session.rb', line 41 def exec(cmd) writer.puts cmd out, * = reader.expect(prompt_pattern) unless out raise "Command `#{cmd}` timed out" end out.each_line.to_a[1..-2].join # remove first line like '> cmd' and last line line '> ' end |
#exec_with_passwords(cmd) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rtprov/session.rb', line 52 def exec_with_passwords(cmd) writer.puts cmd reader.expect(/^Login Password: /) writer.puts router.anonymous_password reader.expect(/^Administrator Password: /) writer.puts router.administrator_password writer.puts "console prompt '#{prompt_prefix}'" # load config may change prompt prefix out, * = reader.expect(prompt_pattern) unless out raise "Command `#{cmd}` timed out" end out.each_line.to_a[1..-2].join # remove first line like '> cmd' and last line line '> ' end |