Last active
January 8, 2024 08:11
-
-
Save MrEnder0/5aa16a45cb233f285c649a1923db163f to your computer and use it in GitHub Desktop.
Bash Terminal Gateway in Rust
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
// 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()) | |
} | |
} |
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
Use this in https://play.rust-lang.org