我正在构建一个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方法,但是我找不到任何关于如何使用它的示例。有什么想法吗?
发布于 2017-11-14 00:14:15
我相信你想要的东西可以做到:
shell = sess.shell.open(:pty => true)
...
shell.exec! queue.pop ...希望这能有所帮助
https://stackoverflow.com/questions/24465897
复制相似问题