2012-04-24 61 views
0

我有一個函數如果使用Net :: SSH庫,如何獲得執行命令的結果?

def run_through_ssh(command) 
    host = $edumate_server 
    user = 'user' 
    pass = '******' 

    output = Array.new  
    Net::SSH.start(host, user, :password => pass) do|ssh| 
     output = ssh.exec(command) 
     #output = ssh.exec(command+" 2>&1") 
    end 

    return output 
end 

其執行我想正確的遠程服務器上的命令,但output變量不包含什麼遠程執行命令輸出到屏幕上。我在sinatra裏面使用這個函數,奇怪的是我可以在屏幕上看到輸出sinatra的輸出。

如何捕獲遠程執行命令的輸出?

輸出變量包含像使用

  • 紅寶石1.8.7(2010-08-16 PATCHLEVEL 302)[I386-的mingw32]
  • 淨-SSH

    #<Net::SSH::Connection::Channel:0x3fe40c0 @remote_maximum_window_size=2097152, @ 
    eof=false, @on_open_failed=nil, @remote_window_size=2097152, @closing=true, @pro 
    perties={}, @local_maximum_packet_size=65536, @on_process=nil, @type="session", 
    @remote_id=0, @on_confirm_open=#<Proc:[email protected]:/Ruby187/lib/ruby/gems/1.8/ge 
    ms/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:320>, @on_request={}, @local_ 
    id=0, @on_extended_data=#<Proc:[email protected]:/Ruby187/lib/ruby/gems/1.8/gems/net- 
    ssh-2.2.1/lib/net/ssh/connection/session.rb:332>, @local_maximum_window_size=131 
    072, @on_eof=nil, @connection=#<Net::SSH::Connection::Session:0x3fe42a0 @options 
    ={:logger=>#<Logger:0x400cb90 @level=4, @progname=nil, @logdev=#<Logger::LogDevi 
    

    東西(2.2 .1)

回答