chore: add ssh_utils
This commit is contained in:
parent
c4723576b7
commit
adda436b41
10
ssh_utils/.gitignore
vendored
Normal file
10
ssh_utils/.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/target
|
||||||
|
target
|
||||||
|
Cargo.lock
|
||||||
|
.cache
|
||||||
|
.temp
|
||||||
|
.env
|
||||||
|
*.log
|
||||||
|
.DS_Store
|
||||||
|
logs
|
||||||
|
tmp
|
12
ssh_utils/Cargo.toml
Normal file
12
ssh_utils/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "ssh_utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["JesusPerez <jpl@jesusperez.pro>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.40"
|
||||||
|
openssh = "0.8.0"
|
||||||
|
tokio = { version = "1.5.0", features = ["full"] }
|
3
ssh_utils/README.md
Normal file
3
ssh_utils/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
### SSH utils library
|
||||||
|
|
||||||
|
SSH utils library
|
11
ssh_utils/TODO.md
Normal file
11
ssh_utils/TODO.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
### SSH utils library
|
||||||
|
|
||||||
|
SSH utils library
|
||||||
|
|
||||||
|
- [ ] Test, test
|
||||||
|
|
||||||
|
- [ ] Connect witj Extend **config** to support other **data stores**
|
||||||
|
|
||||||
|
- [ ] Add encryption to **config**
|
||||||
|
|
||||||
|
- [ ] Complete implementation for this types by moving some code here.
|
74
ssh_utils/src/lib.rs
Normal file
74
ssh_utils/src/lib.rs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
// use openssh::{Session, KnownHosts};
|
||||||
|
use openssh::*;
|
||||||
|
// use std::io;
|
||||||
|
// use std::process::Stdio;
|
||||||
|
use tokio::io::{AsyncWriteExt,AsyncReadExt};
|
||||||
|
|
||||||
|
/// ssh
|
||||||
|
/// ````rust
|
||||||
|
/// // Prepare address
|
||||||
|
/// let host= String::from("hostname-or-ip");
|
||||||
|
/// let user= String::from("root");
|
||||||
|
/// let port: u16 = 22;
|
||||||
|
/// let addr = format!("ssh://{}@{}:{}",&user,&host,port);
|
||||||
|
///
|
||||||
|
/// // for scp_to_add data content into /tmp/hola
|
||||||
|
/// let task= String::from("scp_to_add");
|
||||||
|
/// let trgt=String::from("/tmp/hola");
|
||||||
|
/// let mut data = String::from("Dime \n");
|
||||||
|
//
|
||||||
|
/// // for ssh ls /tmp
|
||||||
|
/// let task= String::from("ssh");
|
||||||
|
/// let trgt=String::from("ls");
|
||||||
|
/// let mut data = String::from("/tmp");
|
||||||
|
///
|
||||||
|
/// // Call command and "macth" result
|
||||||
|
/// match cmds::ssh(&task, &addr, &trgt, &mut data) {
|
||||||
|
/// Ok(rs) => println!("ssh res: {:?} -> {:?}", rs, &data),
|
||||||
|
/// Err(e) => println!("ssh error: {:?}", e),
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
// #[tokio::main]
|
||||||
|
pub async fn ssh(task: &str, addr: &str, trgt: &str, data: &mut String ) -> anyhow::Result<()> {
|
||||||
|
|
||||||
|
let session = Session::connect(&addr,KnownHosts::Strict).await?;
|
||||||
|
|
||||||
|
if task == "ssh" {
|
||||||
|
let ls = session.command(trgt).arg(data).output().await?;
|
||||||
|
match String::from_utf8(ls.stdout) {
|
||||||
|
Ok(res) => println!("ls : {:?}",&res),
|
||||||
|
Err(e) => println!("Error {:?}",e),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
let mut sftp = session.sftp();
|
||||||
|
match task {
|
||||||
|
"scp_to" => {
|
||||||
|
let mut w = sftp.write_to(trgt).await?;
|
||||||
|
let content = data.as_bytes();
|
||||||
|
w.write_all(content).await?;
|
||||||
|
w.close().await?;
|
||||||
|
},
|
||||||
|
"scp_to_add" => {
|
||||||
|
let mut w = sftp.append_to(trgt).await?;
|
||||||
|
let content = data.as_bytes();
|
||||||
|
w.write_all(content).await?;
|
||||||
|
w.close().await?;
|
||||||
|
},
|
||||||
|
"scp_from" => {
|
||||||
|
let mut r = sftp.read_from(trgt).await?;
|
||||||
|
r.read_to_string(data).await?;
|
||||||
|
// println!("source: {:?}",&data);
|
||||||
|
r.close().await?;
|
||||||
|
},
|
||||||
|
_ => println!("Undefined {:?}",&task),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
session.close().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// println!("SSH error no KeyPair found");
|
||||||
|
// .map_err(|e| {
|
||||||
|
// debug!("e = {:?}", e);
|
||||||
|
// Error::SendError
|
||||||
|
// })?;
|
Loading…
Reference in New Issue
Block a user