【发布时间】:2014-06-28 10:29:41
【问题描述】:
我正在构建一个 raspberry-pi 客户端管理器,这些客户端通过 SSH 反向隧道连接到服务器。我为此使用了Sinatra framework 和net-ssh 库。对于单个命令,这很明显,我使用了服务器端事件,将命令发送到给定的 url。在那个动作中,命令被推送到队列,并异步执行,然后只要数据可用,结果就会被推送到事件流连接,这里是命令运行器的伪代码:
require 'net/ssh'
# start a separate thread avoid blocking the main thread of the server.
Thread.start do
sess = Net::SSH.start(HOST, USER, :password => 'pass', :port => 'port')
# once a command is pushed to the queue on the dedecated sinatra action
# the command get executed, once the execution ends this will start listening
# again
loop{
sess.exec! (queue.pop()) do|ch, stream, data|
#this is the server-sent events stream, here the data is pushed to the client
#EventSource
stream_event_connection << data
end
}
end
对于单个命令,这是可以的,例如像“ls /root”这样的命令。但是我需要的是打开一个实时终端(Interective shell),用户可以在其中输入命令并获得结果,就像它在真正的 ssh 终端上一样。我可能会在远程 Respberry-PI 客户端上打开一个 pty,并使用expect 库,这将是可能的。
对于请求 pty,他们是 Net::SSH::Connection::Channel#request_pty 方法,但我找不到任何关于如何使用它的示例。有什么想法吗?
【问题讨论】: