Ruby - problems with Expect and Pty

I am trying to write a Ruby script that will go to the server, run the given command and extract the output from it. Here is what I have so far, mostly adapted from Programming Ruby :

require 'pty' require 'expect' $expect_verbose = true PTY.spawn("ssh root@x.y ") do |reader, writer, pid| reader.expect(/ root@x.y password:.*/) writer.puts("password") reader.expect(/.*/) writer.puts("ls -l") reader.expect(/.*/) answer = reader.gets puts "Answer = #{answer}" end 

Unfortunately, all I get is the following:

 Answer = .y password: 

Any idea what I did wrong and how to facilitate this?

+4
source share
2 answers

For this, I recommend using the net-ssh cheat: sudo gem install net-ssh : http://net-ssh.rubyforge.org/ssh/v2/api/index.html

The code looks something like this:

 require 'rubygems' require 'net/ssh' Net::SSH.start('your-server', 'username', :password => "password") do |ssh| puts ssh.exec!("ls -la") end 
+7
source

Check out http://www.42klines.com/2010/08/14/what-to-expect-from-the-ruby-expect-library.html - it has some nice examples of using PTY with and without Ruby waiting.

It is often easier for me to use PTY, as I can look at my β€œbuffer” and find out what is happening.

+2
source

All Articles