lib/rtprov/session.rb in rtprov-0.1.0 vs lib/rtprov/session.rb in rtprov-0.1.1

- old
+ new

@@ -2,11 +2,11 @@ require "pty" require "shellwords" module Rtprov class Session - attr_reader :router, :reader, :writer, :prompt, :prompt_pattern + attr_reader :router, :reader, :writer, :prompt_prefix, :prompt_pattern def self.start(router, &block) cmd = [ "ssh", "#{router.user}@#{router.host}", @@ -15,29 +15,29 @@ PTY.getpty(cmd) do |r, w, _pid| w.sync = true r.expect(/password/) w.puts router.password - prompt = r.expect(/^(.*>) /)[1] + prompt_prefix = r.expect(/^(.*)> /)[1] - session = new(router, r, w, prompt) + 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 - def initialize(router, reader, writer, prompt = ">") + def initialize(router, reader, writer, prompt_prefix, prompt_suffix) @router = router @reader = reader @writer = writer - @prompt = prompt.dup.freeze - @prompt_pattern = Regexp.compile("^" + Regexp.escape(prompt) + " ").freeze + @prompt_prefix = prompt_prefix.dup.freeze + @prompt_pattern = Regexp.compile("^" + Regexp.escape(prompt_prefix) + "[a-z1-9]*" + prompt_suffix + " ").freeze end def exec(cmd) writer.puts cmd out, * = reader.expect(prompt_pattern) @@ -56,11 +56,11 @@ writer.puts router.anonymous_password reader.expect(/^Administrator Password: /) writer.puts router.administrator_password - writer.puts "console prompt '#{prompt.gsub(/\#$/, "")}'" # load config may change prompt + 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 @@ -74,14 +74,14 @@ 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 = self.class.new(router, reader, writer, "RTPROV", "#") session.exec "console prompt RTPROV" block.call(session) ensure - writer.puts "console prompt '#{prompt.gsub(/>$/, "")}'" + writer.puts "console prompt '#{prompt_prefix}'" reader.expect(/^.*# /) end writer.puts "exit" reader.expect "Save new configuration ? (Y/N)"