Skip to content

Instantly share code, notes, and snippets.

@MrEnder0
Last active January 8, 2024 08:11
Show Gist options
  • Save MrEnder0/5aa16a45cb233f285c649a1923db163f to your computer and use it in GitHub Desktop.
Save MrEnder0/5aa16a45cb233f285c649a1923db163f to your computer and use it in GitHub Desktop.
Bash Terminal Gateway in Rust
// Code written by Mr.Ender for educational purposes only, version 2
use std::{process::Command, io};
fn main() {
let host_name = Command::new("sh")
.arg("-c")
.arg("hostname")
.output()
.expect("failed to get host name");
let host_name_string = String::from_utf8(host_name.stdout).unwrap();
let terminal_prompt = format!("{}@Ubuntu:~$ ", &host_name_string[0..host_name_string.len() - 1]);
println!("{}", terminal_prompt);
loop {
let mut buffer = String::new();
let _ = io::stdin().read_line(&mut buffer);
let output = Command::new("sh")
.arg("-c")
.arg(buffer.clone())
.output()
.expect("failed to execute process");
println!("{}{}", terminal_prompt, buffer);
println!("{}", String::from_utf8(output.stdout).unwrap())
}
}
@MrEnder0
Copy link
Author

MrEnder0 commented Jan 5, 2024

@MrEnder0
Copy link
Author

MrEnder0 commented Jan 6, 2024

I updated the code to fix the bash prompt thingy to show the commands you entered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment