2012-06-20 36 views
0

我如何从.execute方法的Net-SSH-壳牌Ruby Net-SSH-Shell从执行中获得标准!方法

具有良好的“醇净-SSH标准输出,这很容易

Net::SSH.start('host','user') do |ssh| 
    puts ssh.exec! 'date' 
end 

给我Tue Jun 19 23:43:53 EDT 2012

但如果我尝试使用外壳,我得到一个过程对象

Net::SSH.start('host','user') do |ssh| 
    ssh.shell do |bash| 
     output = bash.execute! 'ls' 
     puts output 
    end 
end 

给我#<Net::SSH::Shell::Process:0x007fc809b541d0>

我无法在稀疏文档中找到关于如何轻松获得标准的任何内容。还有就是on_output方法,但似乎并没有为我做任何事情,如果我用execute!方法

我也试过路过一个块.execute!

bash.execute! 'ls' do |output| 
    puts output 
end 

,但我仍然得到过程#<Net::SSH::Shell::Process:0x007fc809b541d0>

我需要标准输出的变量,我可以使用,我需要一个实际的有状态的登录shell,其准系统Net-SSH不会做我的知识。

任何想法?

编辑

沿@vikhyat的建议同样的想法,我想

ssh.shell do |bash| 
     process = bash.execute! 'ls' do |a,b| 
      # a is the process itself, b is the output 
      puts [a,b] 
      output = b 
     end 
end 

b总是空的,甚至当我知道这个命令返回结果。

回答

3

你试过这样做吗?

Net::SSH.start('host','user') do |ssh| 
    ssh.shell do |bash| 
     process = bash.execute! 'ls' 
     process.on_output do |a, b| 
      # a is the process itself, b is the output 
      p [a, b] 
     end 
    end 
end 

你可以看看的Net :: SSH ::过程这里的定义是:https://github.com/mitchellh/net-ssh-shell/blob/master/lib/net/ssh/shell/process.rb

编辑

我觉得问题在execute!出在!因为以下对我很好:

require 'net/ssh' 
require 'net/ssh/shell' 

Net::SSH.start('students.iitmandi.ac.in', 'k_vikhyat') do |ssh| 
    ssh.shell do |bash| 
    process = bash.execute 'whoami' 
    process.on_output do |a, b| 
     p b 
    end 
    bash.wait! 
    bash.execute! 'exit' 
    end 
end 

我不确定为什么会出现这种情况,因为因为它看起来像execute!创建一个进程,运行wait!,然后返回该过程。

+0

不幸的是,我有。传递给'.on_output'的块永远不会被调用。虽然 – wmarbut

+0

hidden_​​premise:请查看我的编辑答案。 – vikhyat

+0

我对你的修改后的答案做了一个小修改,包括'bash.wait!'和'bash.execute! '出口',但这是票。谢谢! – wmarbut

1

我使用的是未来包装

def ssh_exec!(ssh, command) 
    stdout_data = "" 
    stderr_data = "" 
    exit_code = nil 
    exit_signal = nil 

    ssh.open_channel do |channel| 
     channel.exec(command) do |ch, success| 
     unless success 
      abort "FAILED: couldn't execute command (ssh.channel.exec)" 
     end 

     channel.on_data do |ch,data| 
      stdout_data+=data 
     end 

     channel.on_extended_data do |ch,type,data| 
      stderr_data+=data 
     end 

     channel.on_request("exit-status") do |ch,data| 
      exit_code = data.read_long 
     end 

     channel.on_request("exit-signal") do |ch, data| 
      exit_signal = data.read_long 
     end 
     end 
    end 
    ssh.loop 
    [stdout_data, stderr_data, exit_code, exit_signal] 
    end 

与用法:

Net::SSH.start(@ssh_host, @ssh_user, :password => @ssh_password) do |ssh| 
    result = ssh_exec! ssh, command 
end 

在SO或其他地方的某个时候发现了它,现在不记得了。

+0

我尝试使用来自http://net-ssh.github.com/ssh/v2/api/classes/Net/SSH/Connection/Channel.html和http://www.jayway.com/2012的代码/ 01/12/capistrano-and-netssh-with-login-shell /,但没有任何运气获得真正的登录shell ...感谢您的回应! – wmarbut

+0

尝试使用我的方法..它的工作!如果没有,请写下你的错误..也许这是别的东西阻塞你的SSH ..防火墙,我不知道。 – Ribtoks

+0

会做,我会在完成后发布结果。总是善于拥有不止一种好的做事方式。明天可能会有机会。谢谢! – wmarbut