2012-06-20 40 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