ssh-rs ✨
English | 简体中文
Rust implementation of ssh2.0 client.
If you encounter any problems in use, welcome issues
or PR .
Connection method:
1. Password:
use ssh_rs::{Session, ssh};
fn main() {
let mut session: Session = ssh::create_session();
session.set_user_and_password("user", "password");
session.connect("ip:port").unwrap();
}
2. Public key:
Currently, only RSA-PKCS#1-PEM
type encrypted files with the encryption format -----BEGIN RSA PRIVATE KEY-----
are supported.
1. Use key file path:
use ssh_rs::{Session, ssh};
use ssh_rs::key_pair::KeyPairType;
fn main() {
let mut session: Session = ssh::create_session();
session.set_user_and_key_pair_path("user", "pem format key path", KeyPairType::SshRsa).unwrap();
session.connect("ip:port").unwrap();
}
2. Use key string:
use ssh_rs::{Session, ssh};
use ssh_rs::key_pair::KeyPairType;
fn main() {
let mut session: Session = ssh::create_session();
session.set_user_and_key_pair("user", "pem format key string", KeyPairType::SshRsa).unwrap();
session.connect("ip:port").unwrap();
}
Enable global logging:
use ssh_rs::{Session, ssh};
fn main() {
ssh::is_enable_log(true);
let mut session: Session = ssh::create_session();
session.set_user_and_password("user", "password");
session.connect("ip:port").unwrap();
}
Set timeout:
use ssh_rs::{Session, ssh};
fn main() {
let mut session: Session = ssh::create_session();
session.set_timeout(15);
session.set_user_and_password("user", "password");
session.connect("ip:port").unwrap();
}
How to use:
Currently only supports exec shell scp these three functions.
1. exec
use ssh_rs::{ChannelExec, Session, ssh};
fn main() {
let mut session: Session = session();
let exec: ChannelExec = session.open_exec().unwrap();
let vec: Vec<u8> = exec.send_command("ls -all").unwrap();
println!("{}", String::from_utf8(vec).unwrap());
let channel = session.open_channel().unwrap();
let exec = channel.open_exec().unwrap();
let vec: Vec<u8> = exec.send_command("ls -all").unwrap();
println!("{}", String::from_utf8(vec).unwrap());
session.close().unwrap();
}
2. shell
use std::thread::sleep;
use std::time::Duration;
use ssh_rs::{Channel, ChannelShell, Session, ssh};
fn main() {
let mut session: Session = session();
let mut shell: ChannelShell = session.open_shell().unwrap();
run_shell(&mut shell);
let channel: Channel = session.open_channel().unwrap();
let mut shell = channel.open_shell().unwrap();
run_shell(&mut shell);
shell.close().unwrap();
session.close().unwrap();
}
fn run_shell(shell: &mut ChannelShell) {
sleep(Duration::from_millis(500));
let vec = shell.read().unwrap();
println!("{}", String::from_utf8(vec).unwrap());
shell.write(b"ls -all\n").unwrap();
sleep(Duration::from_millis(500));
let vec = shell.read().unwrap();
println!("{}", String::from_utf8(vec).unwrap());
}
3. scp
use ssh_rs::{Channel, ChannelScp, Session, ssh};
fn main() {
let mut session: Session = session();
let scp: ChannelScp = session.open_scp().unwrap();
scp.upload("local path", "remote path").unwrap();
let scp: ChannelScp = session.open_scp().unwrap();
scp.download("local path", "remote path").unwrap();
let channel: Channel = session.open_channel().unwrap();
let scp: ChannelScp = channel.open_scp().unwrap();
scp.upload("local path", "remote path").unwrap();
let channel: Channel = session.open_channel().unwrap();
let scp: ChannelScp = channel.open_scp().unwrap();
scp.download("local path", "remote path").unwrap();
session.close().unwrap();
}
Algorithm support:
1. Kex algorithms
curve25519-sha256
ecdh-sha2-nistp256
2. Server host key algorithms
ssh-ed25519
ssh-rsa
3. Encryption algorithms (client to server)
chacha20-poly1305@openssh.com
aes128-ctr
4. Encryption algorithms (server to client)
chacha20-poly1305@openssh.com
aes128-ctr
5. Mac algorithms (client to server)
hmac-sha1
6. Mac algorithms (server to client)
hmac-sha1
7. Compression algorithms (client to server)
none
8. Compression algorithms (server to client)
none
☃️ Additional algorithms will continue to be added.