Skip to main content

devops_armory/rustible/vm/vm_remote/
vm_remote.rs

1use crate::rustible::vm::{
2    client::client::Session,
3    models::Cli};
4use clap::Parser;
5use anyhow::{Ok, Result};
6
7/// Function to interact with remote server
8pub async fn exec_command_on_remote(user: String, ssh_key_path: String, ip_list: Vec<String>, commands: Vec<String>) -> Result<()> {
9
10    let username = user.to_string();
11    let private_key = ssh_key_path.to_string();
12
13    let ip_s = ip_list.clone(); 
14    let command_s = commands.clone();
15
16    for i in ip_s.iter() {
17        for c in command_s.iter() {
18            println!("{:#?}", i);
19            let mut ssh = Session::connect(
20                private_key.clone(),
21                username.clone(),
22                (i.to_string(), 22),
23                ).await?;
24            let code = ssh
25                .call(
26                    c
27                ).await?;
28            println!("Exitcode: {:?}", code);
29        }
30    }
31
32    Ok(())
33
34}
35
36/// Function to interact with remote server via cli
37/// Host and Commands are Vec<String>
38/// Additional arguments are separated by ","
39pub async fn exec_command_on_remote_cli() -> Result<()> {
40
41    let cli = Cli::parse();
42
43    let username = cli.username;
44    let private_key = cli.private_key;
45
46    let ip_s = cli.host; 
47    let command_s = cli.command;
48
49    for i in ip_s.iter() {
50        for c in command_s.iter() {
51            println!("{:#?}", i);
52            let mut ssh = Session::connect(
53                private_key.clone(),
54                username.clone(),
55                (i.to_string(), 22),
56                ).await?;
57            let code = ssh
58                .call(
59                    c
60                ).await?;
61            println!("Exitcode: {:?}", code);
62        }
63    }
64
65    Ok(())
66
67}