ruby net ssh登录shell

ruby net ssh登录shell,ruby,bash,ssh,net-ssh,Ruby,Bash,Ssh,Net Ssh,有没有办法使用netssh在ruby中获得一个登录shell? 这可能吗 所谓登录shell,我指的是那些源代码/etc/profile..netssh级别太低,无法直接提供此功能(不管怎样,现在是这样)。您可以查看构建在Net SSH基础上的Net SSH Shell,以添加登录Shell功能: 这个实现是可靠的,可以正常工作,但是我发现它不是很有用,因为您无法具体提取诸如stderr或exit status之类的内容,因为命令在子shell中运行,所以只能获取stdout。netsshshe

有没有办法使用netssh在ruby中获得一个登录shell? 这可能吗


所谓登录shell,我指的是那些源代码/etc/profile..

netssh级别太低,无法直接提供此功能(不管怎样,现在是这样)。您可以查看构建在Net SSH基础上的Net SSH Shell,以添加登录Shell功能:

这个实现是可靠的,可以正常工作,但是我发现它不是很有用,因为您无法具体提取诸如stderr或exit status之类的内容,因为命令在子shell中运行,所以只能获取stdout。netsshshell库使用了一些技巧来获取退出状态

我自己的Ruby项目需要一个“登录shell”,为此,我通常使用以下代码直接在shell中执行操作:

def execute_in_shell!(commands, shell="bash")
  channel = session.open_channel do |ch|
    ch.exec("#{shell} -l") do |ch2, success|
      # Set the terminal type
      ch2.send_data "export TERM=vt100\n"

      # Output each command as if they were entered on the command line
      [commands].flatten.each do |command|
        ch2.send_data "#{command}\n"
      end

      # Remember to exit or we'll hang!
      ch2.send_data "exit\n"

      # Configure to listen to ch2 data so you can grab stdout
    end
  end

  # Wait for everything to complete
  channel.wait
end
使用此解决方案,您仍然无法获得登录shell中运行的命令的退出状态或stderr,但至少这些命令是在该上下文中执行的


我希望这能有所帮助。

现在有更好的方法。相反,您可以使用带有pty的shell子系统,从shell登录中获得所需的一切:

Net::SSH.start(@config.host, @config.user, :port => @config.port, :keys => @config.key, :config => true) do |session|
  session.open_channel do |channel|
    channel.request_pty
    channel.send_channel_request "shell" do |ch, success|
      if success
        ch.send_data "env\n"
        ch.send_data "#{command}\n"
        ch.on_data do |c, data|
          puts data
        end
      end
      channel.send_data "exit\n"

      channel.on_close do
        puts "shell closed"
      end
    end
  end
end

结束

这肯定是可能的,但不确定如何在API中实现。可能是
开放频道
?很难说。不起作用,我还是没有登录shell。事实上,我希望这能起作用。好的项目,只有几年的历史,还有一些拉请求。