我正在尝试以下示例代码: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
andecho
命令的一些输出,但我没有看到它。此外,程序/脚本只是挂在最后。我在 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>'