#pty #automation #testing #expect #pexpect

dev rexpect

Interact with unix processes/bash the same way as pexpect or Don libes expect does

5 releases (3 breaking)

0.4.0 May 25, 2020
0.3.0 Feb 25, 2018
0.2.0 Sep 27, 2017
0.1.1 Aug 19, 2017
0.1.0 Jul 27, 2017

#103 in Unix APIs

Download history 802/week @ 2021-08-17 705/week @ 2021-08-24 458/week @ 2021-08-31 574/week @ 2021-09-07 640/week @ 2021-09-14 856/week @ 2021-09-21 655/week @ 2021-09-28 848/week @ 2021-10-05 908/week @ 2021-10-12 572/week @ 2021-10-19 674/week @ 2021-10-26 725/week @ 2021-11-02 617/week @ 2021-11-09 644/week @ 2021-11-16 633/week @ 2021-11-23 787/week @ 2021-11-30

2,790 downloads per month
Used in less than 12 crates

MIT license

772 lines


Build Status crates.io Released API docs Master API docs

Spawn, control, and respond to expected patterns of child applications and processes, enabling the automation of interactions and testing. Components include:

  • session: start a new process and interact with it; primary module of rexpect.
  • reader: non-blocking reader, which supports waiting for strings, regex, and EOF.
  • process: spawn a process in a pty.

The goal is to offer a similar set of functionality as pexpect.


For more examples, check the examples directory.

Basic usage

Add this to your Cargo.toml

rexpect = "0.3"

Simple example for interacting via ftp:

extern crate rexpect;

use rexpect::spawn;
use rexpect::errors::*;

fn do_ftp() -> Result<()> {
    let mut p = spawn("ftp speedtest.tele2.net", Some(30_000))?;
    p.exp_regex("Name \\(.*\\):")?;
    p.send_line("cd upload")?;
    p.exp_string("successfully changed.\r\nftp>")?;
    p.exp_regex("[0-9]+ \"/upload\"")?;

fn main() {
    do_ftp().unwrap_or_else(|e| panic!("ftp job failed with {}", e));

Example with bash and reading from programs

extern crate rexpect;
use rexpect::spawn_bash;
use rexpect::errors::*;

fn do_bash() -> Result<()> {
    let mut p = spawn_bash(Some(2000))?;
    // case 1: wait until program is done
    let hostname = p.read_line()?;
    p.wait_for_prompt()?; // go sure `hostname` is really done
    println!("Current hostname: {}", hostname);

    // case 2: wait until done, only extract a few infos
    p.send_line("wc /etc/passwd")?;
    // `exp_regex` returns both string-before-match and match itself, discard first
    let (_, lines) = p.exp_regex("[0-9]+")?;
    let (_, words) = p.exp_regex("[0-9]+")?;
    let (_, bytes) = p.exp_regex("[0-9]+")?;
    p.wait_for_prompt()?; // go sure `wc` is really done
    println!("/etc/passwd has {} lines, {} words, {} chars", lines, words, bytes);

    // case 3: read while program is still executing
    p.execute("ping", "bytes of data")?; // returns when it sees "bytes of data" in output
    for _ in 0..5 {
        // times out if one ping takes longer than 2s
        let (_, duration) = p.exp_regex("[0-9. ]+ ms")?;
        println!("Roundtrip time: {}", duration);

fn main() {
    do_bash().unwrap_or_else(|e| panic!("bash job failed with {}", e));

Example with bash and job control

One frequent bitfall with sending ctrl-c and friends is that you need to somehow ensure that the program has fully loaded, otherwise the ctrl-* goes into nirvana. There are two functions to ensure that:

  • execute where you need to provide a match string which is present on stdout/stderr when the program is ready
  • wait_for_prompt which waits until the prompt is shown again
extern crate rexpect;
use rexpect::spawn_bash;
use rexpect::errors::*;

fn do_bash_jobcontrol() -> Result<()> {
    let mut p = spawn_bash(Some(1000))?;
    p.execute("ping", "bytes of data")?;
    // bash writes 'ping' to stdout again to state which job was put into background
    p.execute("bg", "ping")?;
    p.send_line("sleep 0.5")?;
    // bash writes 'ping' to stdout again to state which job was put into foreground
    p.execute("fg", "ping")?;
    p.exp_string("packet loss")?;

fn main() {
    do_bash_jobcontrol().unwrap_or_else(|e| panic!("bash with job control failed with {}", e));

Project Status

Rexpect covers more or less the features of pexpect. If you miss anything I'm happy to receive PRs or also Issue requests of course.

The tests cover most of the aspects and it should run out of the box for rust stable, beta and nightly on both Linux or Mac.

Design decisions

  • use error handling of error-chain
  • use nix (and avoid libc wherever possible) to keep the code safe and clean
  • sadly, expect is used in rust too prominently to unwrap Options and Results, use exp_* instead

Licensed under MIT License


~137K SLoC