ruby net-ssh登录外壳 [英] ruby net-ssh login shell

查看:72
本文介绍了ruby net-ssh登录外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以使用net-ssh在ruby中获得登录shell吗? 那有可能吗?

Is there any way i can get a login shell in ruby using net-ssh? Is that even possible?

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

By login shell i mean the ones the source /etc/profile..

推荐答案

Net-SSH级别太低,无法简单地预先提供(无论如何,现在还是这样).您可以签出基于Net-SSH的Net-SSH-Shell来添加登录Shell功能: https://github.com/mitchellh/net-ssh-shell

Net-SSH is too low level to simply provide this up front (the way it is now, anyways). You can check out Net-SSH-Shell which builds upon Net-SSH to add login shell functionality: https://github.com/mitchellh/net-ssh-shell

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

The implementation is solid and works, however I found its not too useful since you can't specifically extract things like stderr or exit status because the commands run in a sub-shell, so you can only get stdout. The net-ssh-shell library uses some hacks to get the exit status.

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

I've needed a "login shell" for my own Ruby projects and to do this I've generally executed things directly into the shell using the following code:

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进入登录外壳程序,但是至少命令是在该上下文中执行的.

With this solution you still don't get exit status or stderr of commands run into the login shell, but at least the commands are executed in that context.

我希望这会有所帮助.

这篇关于ruby net-ssh登录外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆