1

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

1 回答 1

1

The answer here is that you will not see output except for what you have determined you want output for. Then you need to direct the return of the command to be rendered or output, in the normal ways.

In this code, there are only two lines of output, and they are:

puts "Exit Status:#{p.exit_status}"

puts "Command Executed:#{p.command}"

I believe you are seemingly not getting progression on the program due to it waiting for a password. When I run this code and have key-only ssh access, it continues on. When it is connecting to my local system, it is seemingly waiting for my password.

This is a ssh session, you will need to exit the session at some point, when you want to leave it.

Still you want output from the session you will need to do the same thing as you do for your output on hostname. You used puts. You also do the same for the last two I mentioned above.

It isn't hanging, it simply is not told you are done with the secure shell.

于 2013-06-28T15:40:54.693 回答