1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use fs::File;
use std::{
fs,
io::{Read, Write},
net::{TcpStream, ToSocketAddrs},
path::{Path, PathBuf},
};
use diagnostic_quick::{error_3rd::SSH2Session, QError, QResult};
/// Connect to a remote computer via the ssh protocol.
pub struct SwarmSSH {
session: SSH2Session,
}
pub mod scp;
pub mod shell;
impl SwarmSSH {
/// Log in to the remote computer with username and password
///
/// # Arguments
///
/// * `address`: The address of the remote computer, such as `192.168.1.100:22`
/// * `user`: The username of the remote computer, such as `root`
/// * `password`: The password of the remote computer, such as `password`
///
/// # Examples
///
/// ```rust, no_run
/// # use diagnostic_quick::QResult;
/// # use swarm_ssh::SwarmSSH;
/// async fn test_password() -> QResult<SwarmSSH> {
/// SwarmSSH::login_password("192.168.1.100:22", "root", "password").await
/// }
/// ```
pub async fn login_password<A>(address: A, user: &str, password: &str) -> QResult<Self>
where
A: ToSocketAddrs,
{
let tcp = TcpStream::connect(address)?;
let mut session = SSH2Session::new()?;
session.set_tcp_stream(tcp);
session.handshake()?;
session.userauth_password(user, password)?;
if !session.authenticated() {
Err("Authentication failed")?
}
Ok(Self { session })
}
}