ruby net-ssh登录shell
发布时间:2020-12-16 21:56:03 所属栏目:百科 来源:网络整理
导读:有什么办法可以使用net-ssh在 ruby中获取登录shell吗? 这甚至可能吗? 通过登录shell我的意思是源/ etc / profile .. 解决方法 Net-SSH的级别太低而不能简单地提供这个(现在的方式,无论如何).您可以查看基于Net-SSH构建的Net-SSH- Shell,以添加登录shell功
有什么办法可以使用net-ssh在
ruby中获取登录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=vt100n" # 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 "exitn" # Configure to listen to ch2 data so you can grab stdout end end # Wait for everything to complete channel.wait end 使用此解决方案,您仍然无法获得退出状态或stderr命令进入登录shell,但至少命令在该上下文中执行. 我希望这有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |