次のサンプルコードを試しています: http://www.ruby-doc.org/gems/docs/n/net-ssh-shell-0.2.0/README_rdoc.html
require 'net/ssh'
require 'net/ssh/shell'
Net::SSH::start('host', 'user', :password=>'password') do |ssh|
puts ssh.exec!("hostname")
ssh.shell do |sh|
sh.execute "cd /usr/local"
sh.execute "pwd"
sh.execute "export FOO=bar"
sh.execute "echo $FOO"
p=sh.execute "grep dont /tmp/notexist"
puts "Exit Status:#{p.exit_status}"
puts "Command Executed:#{p.command}"
end
end
パスワードを渡し、 でテストしてログインしていることを確認しましたssh.exec!("hostname")
。pwd
andコマンドからの出力を期待していecho
ますが、表示されません。さらに、プログラム/スクリプトは最後にハングします。Windows で Ruby 1.9.3p429 (2013-05-15) [i386-mingw32] を使用しています。OS X と Ubuntu の SSH サーバーに対してテストしました。
ここに私の出力があります。トレースは、Ctrl-C を実行した私からのものです。
C:\sb>ruby test.sh
Exit Status:
Command Executed:grep dont /tmp/notexist
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/ruby_compat.rb:22:
in `select': Interrupt
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/ruby_compat.rb:22:in `io_select'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:201:in `process'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `block in loop'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `loop'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `loop'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:110:in `close'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh.rb:191:in `start'
from test.sh:5:in `<main>'