-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/bin/sh
output is not what expected
#15
Comments
Hi @the-ssd could you provide the code your using? |
PS: I've also had some plans to make a SSH api zhiburt/expectrl#23, but after looking through some analogues I got to worry that it's harder then it seems. |
use std::thread;
use std::time::Duration;
use ptyprocess::PtyProcess;
use std::io::{BufReader, Result, Write, Read};
use std::process::Command;
fn main() -> Result<()> {
// spawn a cat process
let process = PtyProcess::spawn(Command::new("/bin/sh"))?;
// stream for reading
let stream = process.get_raw_handle()?;
// thread for reading (needed for ssh to work)
thread::spawn(move || {
let mut reader = BufReader::new(stream);
loop {
let mut buf = [0; 1];
let _ = reader.read_exact(&mut buf);
// in ssh this would be set data to client
print!("{}", String::from_utf8_lossy(&buf));
}
});
// create a new communication stream for writing
let mut stream = process.get_raw_handle()?;
thread::sleep(Duration::from_secs(1));
// send a message to process
writeln!(stream, "echo test")?;
// wait to read output from the stream
thread::sleep(Duration::from_secs(1));
Ok(())
} |
Also, I have a working ssh server (works with OpenSSH), but I am having problems with the client |
So I've seemed to figured it out. let mut process = PtyProcess::spawn(Command::new("/bin/sh"))?;
process.set_echo(true, None).unwrap(); |
PS: I'd recommend you to use |
Thanks, this solved the first problem, but there is still the other one use std::thread;
use std::time::Duration;
use ptyprocess::PtyProcess;
use std::io::{BufReader, Result, Write, Read};
use std::process::Command;
fn main() -> Result<()> {
// spawn a process
let mut process = PtyProcess::spawn(Command::new("/bin/sh"))?;
process.set_echo(true, None).unwrap();
// stream for reading
let stream = process.get_pty_stream()?;
// thread for reading (needed for ssh to work)
thread::spawn(move || {
let mut reader = BufReader::new(stream);
loop {
let mut buf = [0; 1];
let _ = reader.read_exact(&mut buf);
// in ssh this would be set data to client
print!("{}", String::from_utf8_lossy(&buf));
}
});
// wait to read output from the stream
thread::sleep(Duration::from_secs(5));
Ok(())
} this should return |
Seems like it works for me?
Maybe you shall increase the delay. |
PS: I am not sure what you're trying to achieve with additional thread. But you could use use expectrl::repl::ReplSession;
fn main() {
let mut p = expectrl::spawn("sh").unwrap();
p.get_process_mut().set_echo(true, None).unwrap();
let mut shell = ReplSession::new(p, String::from("sh-5.1$"), Some(String::from("exit")), true);
shell.expect_prompt().unwrap();
let output = exec(&mut shell, "echo Hello World");
println!("{:?}", output);
let output = exec(&mut shell, "echo '2 + 3' | bc");
println!("{:?}", output);
}
fn exec(shell: &mut ReplSession, cmd: &str) -> String {
let buf = shell.execute(cmd).unwrap();
let mut string = String::from_utf8_lossy(&buf).into_owned();
string = string.replace("\r\n\u{1b}[?2004l\r", "");
string = string.replace("\r\n\u{1b}[?2004h", "");
string
}
|
Some shells have autocomplete so it will be empty then write |
True But not exactly, I've just got to remember that terminals often do some buffering.
Sorry, I don't get it, could you explain a bit? |
So to run a remote shell that has autocomplete, I can't just wait for the full command because then autocomplete doesn't work. Changing std::io::stdout().flush().unwrap(); // this also worked |
expectation:
reality:
(this works with fish instead of sh)
also when starting shell without writing anything to it, you would expect
but instead it is empty (same on fish)
this is important for ssh, which is what I am trying to do
there is
portabe-pty
(I am currently using it), but it has a lot of dependencies (includes nix)and because I am writing it for Redox OS
I will need to port multiple dependencies, some of which are 4–6 years old
while for
ptyprocess
it's just nix (still needs porting)if you could fix these 2 problems it will be really helpful :)
The text was updated successfully, but these errors were encountered: