ruby net-ssh登录shell

有什么办法可以使用net-sshruby获取登录shell吗?
这甚至可能吗?

通过登录shell我的意思是源/ etc / profile ..

解决方法

net-ssh的级别太低而不能简单地提供这个(现在的方式,无论如何).您可以查看基于net-ssh构建的net-ssh- Shell,以添加登录shell功能https://github.com/mitchellh/net-ssh-shell

实现是可靠的并且有效,但是我发现它不太有用,因为你不能专门提取诸如stderr或退出状态之类的东西,因为命令在子shell中运行,所以你只能得到stdout. net-ssh-shell库使用一些hack来获取退出状态.

我需要一个用于我自己的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

使用此解决方案,您仍然无法获得退出状态或stderr命令进入登录shell,但至少命令在该上下文中执行.

我希望这有帮助.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...