Expand description
The main crate of Rexpect
Overview
Rexpect is a loose port of pexpect which itself is inspired by Don Libe’s expect.
It’s main components (depending on your need you can use either of those)
- session: automate stuff in Rust
- reader: a non-blocking reader with buffering, matching on strings/regex/…
- process: spawn a process in a pty
Basic example
extern crate rexpect;
use rexpect::spawn;
use rexpect::error::Error;
fn main() -> Result<(), Error> {
let mut p = spawn("ftp speedtest.tele2.net", Some(2000))?;
p.exp_regex("Name \\(.*\\):")?;
p.send_line("anonymous")?;
p.exp_string("Password")?;
p.send_line("test")?;
p.exp_string("ftp>")?;
p.send_line("cd upload")?;
p.exp_string("successfully changed.\r\nftp>")?;
p.send_line("pwd")?;
p.exp_regex("[0-9]+ \"/upload\"")?;
p.send_line("exit")?;
p.exp_eof()?;
Ok(())
}
Example with bash
Tip: try the chain of commands first in a bash session.
The tricky thing is to get the wait_for_prompt right.
What wait_for_prompt
actually does is seeking to the next
visible prompt. If you forgot to call this once your next call to
wait_for_prompt
comes out of sync and you’re seeking to a prompt
printed “above” the last execute()
.
extern crate rexpect;
use rexpect::spawn_bash;
use rexpect::error::Error;
fn main() -> Result<(), Error> {
let mut p = spawn_bash(Some(30_000))?;
p.execute("ping 8.8.8.8", "bytes of data")?;
p.send_control('z')?;
p.wait_for_prompt()?;
p.execute("bg", "suspended")?;
p.send_line("sleep 1")?;
p.wait_for_prompt()?;
p.execute("fg", "continued")?;
p.send_control('c')?;
p.exp_string("packet loss")?;
Ok(())
}
Re-exports
pub use reader::ReadUntil;
pub use session::spawn;
pub use session::spawn_bash;
pub use session::spawn_python;
pub use session::spawn_stream;