Connect SSH command to stdout

2019-07-13 16:14发布

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.

How would I SSH into an IP address and then allow the user to interact with the SSH session?

This example would open a ssh session to a machine in the cluster named "platform":

company_name ssh:platform

I've done this before in Ruby like so:

exec("ssh -i #{private_key_file.path} -R 52698:localhost:52698 core@#{platform_ip}")

标签: ssh rust
1条回答
Melony?
2楼-- · 2019-07-13 16:57

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.

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();
查看更多
登录 后发表回答