Expand description
Dependencies
ssh-rs = "*"
Quick example:
use std::io::{stdin, stdout, Write};
use std::sync::{Arc, Mutex};
use std::{thread, time};
use ssh_rs::{Channel, ChannelExec, Session, SSH};
fn main() {
let ssh: SSH = SSH::new();
// enable logging
ssh.enable_log(true).unwrap();
let mut session = ssh.get_session("127.0.0.1:22").unwrap();
session.set_user_and_password("root".to_string(), "123456".to_string());
session.connect().unwrap();
exec(&mut session);
shell(&mut session);
// t_shell(&mut session);
}
fn exec(session: &mut Session) {
let exec: ChannelExec = session.open_exec().unwrap();
let vec = exec.send_command("ls -all").unwrap();
println!("{}", String::from_utf8(vec).unwrap());
}
fn shell(session: &mut Session) {
let mut shell = session.open_shell().unwrap();
thread::sleep(time::Duration::from_millis(200));
let vec = shell.read().unwrap();
let result = String::from_utf8(vec).unwrap();
println!("{}", result);
shell.write(b"ls -a\r").unwrap();
thread::sleep(time::Duration::from_millis(200));
let vec = shell.read().unwrap();
let result = String::from_utf8(vec).unwrap();
println!("{}", result);
shell.close().unwrap();
}
fn t_shell(session: &mut Session) {
let shell = session.open_shell().unwrap();
let c1 = Arc::new(Mutex::new(shell));
let c2 = Arc::clone(&c1);
let t1 = thread::spawn( move || {
loop {
let x = c1.lock().unwrap().read().unwrap();
if x.is_empty() { continue }
stdout().write(x.as_slice()).unwrap();
stdout().flush().unwrap();
}
});
let t2 = thread::spawn( move || {
loop {
let mut cm = String::new();
stdin().read_line(&mut cm).unwrap();
c2.lock().unwrap().write(cm.as_bytes()).unwrap();
}
});
t1.join().unwrap();
t2.join().unwrap();
}