将 SSH 命令连接到标准输出 [英] Connect SSH command to stdout
问题描述
我正在创建一个命令行工具,它可以从我们的集群中动态查找服务器,然后根据集群检索保存在 Amazon s3 存储桶中的相应私钥,并将其复制到本地的临时文件以连接到服务器.
I am creating a command-line tool that dynamically finds a server from our cluster, then based on the cluster retrieves the appropriate private key held in an Amazon s3 bucket and copies it to a tempfile locally to connect to the server.
如何通过 SSH 连接到 IP 地址,然后允许用户与 SSH 会话进行交互?
How would I SSH into an IP address and then allow the user to interact with the SSH session?
此示例将打开一个到名为platform"的集群中一台机器的 ssh 会话:
This example would open a ssh session to a machine in the cluster named "platform":
company_name ssh:platform
我以前在 Ruby 中这样做过:
I've done this before in Ruby like so:
exec("ssh -i #{private_key_file.path} -R 52698:localhost:52698 core@#{platform_ip}")
推荐答案
你可能想看看 ssh2-rs 箱子;文档有一个很好的示例,说明您的情况努力完成.
You probably want to take a look at the ssh2-rs crate; the docs have a good example of what you're trying to accomplish.
它的要点是您获得一个 channel_session 对象,该对象具有从 SSH 控制台读取输出的方法(从此处的文档中复制):
The gist of it is that you get a channel_session object, which has methods to read the outputs from the SSH console (copied from the docs here):
let mut channel = sess.channel_session().unwrap();
channel.exec("ls").unwrap();
let mut s = String::new();
channel.read_to_string(&mut s).unwrap();
这篇关于将 SSH 命令连接到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!