Crates.io | rustssh |
lib.rs | rustssh |
version | 0.1.2 |
source | src |
created_at | 2023-06-03 19:37:43.23121 |
updated_at | 2023-06-04 16:16:20.440537 |
description | ssh tools |
homepage | https://github.com/mylsn/rustssh |
repository | https://github.com/mylsn/rustssh |
max_upload_size | |
id | 881811 |
size | 14,665 |
rust ssh sudo password scp
# Cargo.toml
[dependencies]
rustssh = "0.1.2"
# src/main.rs
use rustssh::{Connection, Auth, SudoOptions};
fn main() {
// create connection
let mut conn = Connection::new("1.1.1.1".to_string(), 22, "username".to_string(), Auth::Password("password".to_string())).unwrap();
// Execute remote commands as the connected user
let (stdout, stderr, status) = conn.run("ls").unwrap();
println!("The output of run is: {}, The outerr of run is: {}, The status code of run is: {}, end.", stdout, stderr, status);
// Execute remote commands as root user
let (stdout, stderr, status) = conn.sudo("ls -l /root/").unwrap();
println!("The output of sudo is: {}, The outerr of sudo is: {}, The status code of sudo is: {}, end.", stdout, stderr, status);
// Execute remote commands as user user01
// SudoOptions::new() Specify three parameters: 1. The username to sudo, 2. The password that needs to be entered when sudo, 3. Specify the sudo prompt
let opt = SudoOptions::new("user01", "", "");
let (stdout, stderr, status) = conn.sudo_with_options("ls -l /home/user01/", Some(opt)).unwrap();
println!("The output of sudo is: {}, The outerr of sudo is: {}, The status code of sudo is: {}, end.", stdout, stderr, status);
// scp copies local files to remote
conn.scp("/home/xxx/x.xml", "/home/xxx/xx.xml").unwrap();
}