Module: Expect4r
- Defined in:
- lib/expect4r.rb,
lib/expect/io.rb,
lib/misc/base.rb,
lib/misc/shell.rb,
lib/router/modes.rb
Defined Under Namespace
Modules: Router Classes: Base, BaseLoginObject, ConnectionError, ExpTimeoutError, Expect4rIO_Error, Ios, Iox, J, RShell, Shell, SpawnError, V
Instance Method Summary collapse
- #_login(cmd = self.spawnee, arg = {}) ⇒ Object (also: #login)
- #child_exit ⇒ Object
- #connected? ⇒ Boolean
- #exp_print(s) ⇒ Object
- #exp_puts(s) ⇒ Object
- #exp_send(lines, arg = {}) ⇒ Object
- #expect(match, ti = 5, matches = []) ⇒ Object
- #get_prompt ⇒ Object
- #getc ⇒ Object
- #interact(k = "C-t") ⇒ Object
- #login_by_proxy(proxy) ⇒ Object (also: #login_via)
- #logout ⇒ Object
- #putc(c) ⇒ Object
- #putcr ⇒ Object
- #read_until(match, ti = 0.2) ⇒ Object
- #readline(ti = 0.2, matches = []) ⇒ Object
- #spawn(cmd) ⇒ Object
Instance Method Details
#_login(cmd = self.spawnee, arg = {}) ⇒ Object Also known as: login
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/expect/io.rb', line 284 def _login(cmd=self.spawnee, arg={}) return if connected? if arg[:proxy] login_proxy cmd, arg else spawn cmd end arg={:timeout=>13, :no_echo=>false}.merge(arg) timeout = arg[:timeout] no_echo = arg[:no_echo] output=[] t0 = Time.now ev, buf = catch(:done) do @r.readbuf(timeout) do |read_pipe| if read_pipe._io_exit? exp_internal "readbuf: _io_exit?" throw :done, [ :cnx_error, read_pipe._io_buf1] end @pre_matches ||= [] @pre_matches.each do |match, _send| if read_pipe._io_string =~ match read_pipe._io_save no_echo, "match #{match}" if _send.is_a?(Proc) _send.call else exp_puts _send.to_s end end end case read_pipe._io_string when spawnee_prompt read_pipe._io_save no_echo, "match PROMPT" throw(:done, [:ok, read_pipe._io_buf1]) when /Last (L|l)ogin:/ read_pipe._io_save no_echo # consumes when /(user\s*name\s*|login):\s*$/i read_pipe._io_save no_echo, "match USERNAME" exp_puts spawnee_username when /password:\s*$/i read_pipe._io_save no_echo, "match PASSWORD" @w.print(spawnee_password+"\r") and flush when /Escape character is/ read_pipe._io_save no_echo, "match Escape char" io_escape_char_cb when /.*\r\n/ exp_internal "match EOL" read_pipe._io_save no_echo, "match EOL" when /Are you sure you want to continue connecting \(yes\/no\)\?/ read_pipe._io_save no_echo, "match continue connecting" exp_puts 'yes' when /(Login incorrect|denied, please try again)/ spawnee_reset else # For objects that include Expect4r but do not subclass base Login class. @matches ||= [] @matches.each { |match, _send| if read_pipe._io_string =~ match read_pipe._io_save no_echo, "match #{match}" if _send.is_a?(Proc) exp_puts _send.call else exp_puts _send end end } end end end case ev when :cnx_error child_exit err_msg = "Could not connect to #{@host}:\n" err_msg += " >> #{cmd}\n " err_msg += buf.join("\n ") raise ConnectionError.new(err_msg) else @_lp_1 = buf[-2] @lp = buf.last end [buf, ev] end |
#child_exit ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/expect/io.rb', line 83 def child_exit if @proxy @proxy.logout else Process.kill(:KILL, @pid) if @pid @thread.kill if @thread end rescue Errno::ESRCH, Errno::ECHILD => e ensure @lp, @r, @w, @pid, @proxy = [nil]*5 end |
#connected? ⇒ Boolean
274 275 276 |
# File 'lib/expect/io.rb', line 274 def connected? @r && (not child_exited?) end |
#exp_print(s) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/expect/io.rb', line 139 def exp_print(s) _retry=self.connect_retry begin exp_internal "print: #{s.inspect}, io_writer: #{@w}" return unless @w @w.print(s) and flush rescue Errno::EIO, Errno::ECHILD child_exit exp_internal "session disconnected: retrying..." raise unless _retry>0 _retry -=1 sleep(1) self.login retry end end |
#exp_puts(s) ⇒ Object
135 136 137 |
# File 'lib/expect/io.rb', line 135 def exp_puts(s) exp_print "#{s}\r" end |
#exp_send(lines, arg = {}) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/expect/io.rb', line 201 def exp_send(lines, arg={}) r=[] lines.each_line do |l| l = l.chomp("\n").chomp("\r").strip r << putline(l, arg) if l.size>0 end lines.size==1 ? r[0] : r end |
#expect(match, ti = 5, matches = []) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/expect/io.rb', line 210 def expect(match, ti=5, matches=[]) t0 = Time.now rc, buf = catch(:done) do @r.readbuf(ti) do |r| if r._io_exit? throw :done, [:abort, r._io_string!] end case r._io_string when match r._io_save false, "matching PROMPT" # puts "debug IO BUF 1 #{r._io_buf1.inspect}" throw(:done, [:ok, r._io_buf1]) # FIXME # Watch out for the ping command !!!! # throw :done, [:ok, r._io_string!.chomp("\r\n")] when /(.+)\r\n/, "\r\n" r._io_save false, "matching EOL" else matches.each do |match, arg| if r._io_string =~ match r._io_save false, "match #{match}" if arg.is_a?(Proc) arg.call(self) else exp_puts arg end end end end end end case rc when :abort elapsed = Time.now - t0 if elapsed < ti child_exit raise ConnectionError.new(buf) else raise ExpTimeoutError.new(buf, elapsed) end else @lp = buf.last end [buf, rc] end |
#get_prompt ⇒ Object
256 257 258 |
# File 'lib/expect/io.rb', line 256 def get_prompt putline '', :no_trim=>true, :no_echo=>true end |
#getc ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/expect/io.rb', line 156 def getc _retry=self.connect_retry begin @r.getc if @r rescue Errno::EIO, Errno::ECHILD child_exit exp_internal "session disconnected: retrying..." raise unless _retry>0 _retry -=1 sleep(1) self.login retry end end |
#interact(k = "C-t") ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/expect/io.rb', line 171 def interact(k="C-t") raise unless k =~ /C\-[A-Z]/i unless STDOUT.tty? and STDIN.tty? $stderr.puts "Cannot interact: not running from terminal!" return end login unless connected? @@interact_mutex ||= Mutex.new @@interact_mutex.synchronize { k.upcase! STDOUT.puts "\n\#\n\# #{k.gsub(/C\-/,'^')} to terminate.\n\#\n" reader :start writer(eval "?\\#{k}") } rescue ensure begin reader :terminate putline ' ', :no_trim=>true, :no_echo=>true rescue => e exp_internal e.to_s end end |
#login_by_proxy(proxy) ⇒ Object Also known as: login_via
278 279 280 281 |
# File 'lib/expect/io.rb', line 278 def login_by_proxy(proxy) raise ArgumentError, "Don't know how to login to #{proxy}" unless proxy.respond_to? :_login_ _login_ :proxy=> proxy end |
#logout ⇒ Object
120 121 122 123 124 |
# File 'lib/expect/io.rb', line 120 def logout child_exit @lp, @_lp_1 = nil,nil @pid end |
#putc(c) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/expect/io.rb', line 126 def putc(c) return unless @w || c.nil? exp_internal "[#{c}]" @w.putc(c) and flush rescue Errno::EIO child_exit raise end |
#putcr ⇒ Object
196 197 198 199 |
# File 'lib/expect/io.rb', line 196 def putcr putline '', :no_trim=>true, :no_echo=>true nil end |
#read_until(match, ti = 0.2) ⇒ Object
267 268 269 270 271 272 |
# File 'lib/expect/io.rb', line 267 def read_until(match,ti=0.2) ret = expect(match, ti, []) ret[0][0].chomp rescue ExpTimeoutError => ex '' end |
#readline(ti = 0.2, matches = []) ⇒ Object
260 261 262 263 264 265 |
# File 'lib/expect/io.rb', line 260 def readline(ti=0.2, matches=[]) ret = expect(/(.+)\r\n/, ti, matches) ret[0][0].chomp rescue ExpTimeoutError => ex '' end |
#spawn(cmd) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/expect/io.rb', line 95 def spawn(cmd) begin child_exited = false @thread = Thread.new do PTY.spawn(cmd) do |pipe_read, pipe_write, pid| @r, @w, @pid = pipe_read, pipe_write, pid begin Process.wait(@pid,0) rescue ensure child_exited = true end end end @thread.priority = -2 unless child_exited while @r.nil? sleep(0.05) end end rescue => e raise SpawnError.new(cmd) end end |