【问题标题】:How to get the result of executed command if Net::SSH library used?如果使用 Net::SSH 库,如何获得执行命令的结果?
【发布时间】:2012-04-24 12:01:39
【问题描述】:

我有一个功能

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 的屏幕上看到输出。

如何捕获远程执行命令的输出?

输出变量包含类似的东西

#<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:0x031f64a0@C:/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:0x031f6560@C:/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

使用

  • ruby 1.8.7(2010-08-16 补丁级别 302)[i386-mingw32]
  • net-ssh (2.2.1)

【问题讨论】:

    标签: ruby ssh rubygems


    【解决方案1】:

    这样的事情应该可以工作:

    Net::SSH.start( host, user, :password => pass ) do |ssh|
      output = ssh.exec!(command)
    end
    output
    

    执行官!如果没有给出块,方法捕获标准输入/标准输出。见http://net-ssh.github.com/ssh/v2/api/classes/Net/SSH/Connection/Session.html#M000094

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 2015-07-06
      • 1970-01-01
      • 2013-05-27
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多