Created
April 28, 2018 00:00
-
-
Save drusellers/a9cc5feaadc5542fcbe1eacd848c8956 to your computer and use it in GitHub Desktop.
Bastion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[macro_use] | |
| extern crate structopt; | |
| use std::process::{Command, Stdio}; | |
| use structopt::clap::AppSettings; | |
| use structopt::StructOpt; | |
| #[derive(StructOpt, Debug)] | |
| // https://docs.rs/clap/2/clap/enum.AppSettings.html#variant.InferSubcommands | |
| #[structopt(raw(setting = "AppSettings::InferSubcommands"))] | |
| enum Opt { | |
| // https://docs.rs/clap/2/clap/struct.App.html#method.alias | |
| #[structopt(name = "start")] | |
| Start, | |
| // https://docs.rs/clap/2/clap/struct.App.html#method.aliases | |
| #[structopt(name = "stop")] | |
| Stop, | |
| #[structopt(name = "restart")] | |
| Restart(Abc), | |
| #[structopt(name = "fetch")] | |
| Fetch, | |
| #[structopt(name = "install")] | |
| Install, | |
| } | |
| #[derive(StructOpt, Debug)] | |
| #[structopt(name = "basic")] | |
| struct Abc { | |
| target_ip: String, | |
| #[structopt(raw(last = "false"))] | |
| command: String, | |
| #[structopt(name = "identity", short = "i")] | |
| identity_file: Option<String>, | |
| // TODO: parse this out with <user>@<ip address> | |
| #[structopt(name = "user", short = "u")] | |
| user: Option<String>, | |
| } | |
| fn main() { | |
| let opt = Opt::from_args(); | |
| println!("{:?}", opt); | |
| println!(""); | |
| match opt { | |
| Opt::Restart(o) => { | |
| let bastion = look_up_bastion(); | |
| // build proxy command | |
| let mut builder = String::new(); | |
| builder.push_str("ProxyCommand='ssh "); | |
| if let Some(id) = o.identity_file.clone() { | |
| builder.push_str(&format!("-i {} ", id)); | |
| } | |
| builder.push_str("-W "); | |
| builder.push_str(&format!("{}:22 ", o.target_ip)); | |
| if let Some(u) = o.user.clone() { | |
| builder.push_str(&u); | |
| builder.push_str("@"); | |
| } | |
| builder.push_str(&bastion); | |
| builder.push_str("'"); | |
| //ProxyCommand='ssh -i <path> -W <private ip>:22 <bastion>' | |
| let proxy_arg = builder; | |
| let mut cmd = Command::new("ssh"); | |
| cmd.arg("-o"); | |
| cmd.arg(proxy_arg); | |
| if let Some(id) = o.identity_file { | |
| cmd.arg("-i"); | |
| cmd.arg(id); | |
| } | |
| if let Some(u) = o.user { | |
| cmd.arg(format!("{}@{}", u, o.target_ip)); | |
| } else { | |
| cmd.arg(o.target_ip); | |
| } | |
| println!("{:?}", cmd); | |
| cmd.stdout(Stdio::inherit()); | |
| let mut handle = cmd.spawn().expect("should ssh"); | |
| let ecode = handle.wait().expect("failed to wait on child"); | |
| println!("exit code {}", ecode); | |
| () | |
| } | |
| _ => (), | |
| } | |
| } | |
| fn look_up_bastion() -> String { | |
| "192.168.0.1".to_string() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment