The goal is to offer a similar set of functionality as pexpect.
Add this to your Cargo.toml
(sorry, not posted to crates.io yet)
toml
[dependencies]
rexpect = {git = "https://github.com/philippkeller/rexpect"}
Simple example for interacting via ftp:
```rust extern crate rexpect;
use rexpect::spawn; use rexpect::errors::*;
fn doftp() -> Result<()> { let mut p = spawn("ftp speedtest.tele2.net", Some(2000))?; p.expregex("Name \(.*\):")?; p.sendline("anonymous")?; p.expstring("Password")?; p.sendline("test")?; p.expstring("ftp>")?; p.sendline("cd upload")?; p.expstring("successfully changed.\r\nftp>")?; p.sendline("pwd")?; p.expregex("[0-9]+ \"/upload\"")?; p.sendline("exit")?; p.expeof()?; Ok(()) }
fn main() { doftp().unwrapor_else(|e| panic!("ftp job failed with {}", e)); } ```
```rust extern crate rexpect; use rexpect::spawn_bash; use rexpect::errors::*;
fn run() -> Result<()> { let mut p = spawnbash(None)?; p.execute("ping 8.8.8.8")?; p.sendcontrol('z')?; p.waitforprompt()?; p.execute("bg")?; p.waitforprompt()?; p.execute("sleep 1")?; p.waitforprompt()?; p.execute("fg")?; p.sendcontrol('c')?; p.expstring("packet loss")?; Ok(()) }
fn main() { run().unwraporelse(|e| panic!("bash process failed with {}", e)); } ```
What already works:
What does not yet work:
What will probably never be implemented
expect
is used in rust too prominently to unwrap Option
s and Result
s, use exp_*
instead