【发布时间】:2018-10-26 19:25:04
【问题描述】:
这不是 How to SSH interactive Session 或 Net::SSH interactive terminal scripting. How? 或 Ruby net-ssh interactive response/reply 的副本
我正在尝试使用 Net:SSH 编写交互式 SSH 客户端,因为我已经使用它在目标主机上运行非交互式命令。
我可以直接使用system "ssh",但这需要将连接设置、代理等转换为ssh 参数。
问题在于将数据从STDIN 流式传输到shell 通道。 listen_to 的 Net::SSH 文档显示了当输入来自套接字而不是 STDIN 时如何执行此操作。 $stdin 或 IO.console 不是套接字,因此与 Net::SSH::BufferedIo 不兼容。
有没有办法从STDIN 创建一个可用于此目的的套接字?或者有没有更好的方法将所有内容从STDIN 发送到Net::SSH::Channel,直到频道关闭?
以下代码有效,但速度太慢而无法使用:
require 'net/ssh'
require 'io/console'
require 'io/wait'
Net::SSH.start('localhost', 'root') do |session|
session.open_channel do |channel|
channel.on_data do |_, data|
$stdout.write(data)
end
channel.on_extended_data do |_, data|
$stdout.write(data)
end
channel.request_pty do
channel.send_channel_request "shell"
end
channel.connection.loop do
$stdin.raw do |io|
input = io.readpartial(1024)
channel.send_data(input) unless input.empty?
end
channel.active?
end
end.wait
end
【问题讨论】: