#expect #terminal #automation #pty

expectrl

A tool for automating terminal applications in Unix like Don libes expect

12 releases (6 breaking)

0.7.1 Jul 3, 2023
0.7.0 Mar 30, 2023
0.6.0 Sep 24, 2022
0.5.2 Jun 8, 2022
0.1.3 Jul 30, 2021

#63 in Testing

Download history 438/week @ 2023-11-20 729/week @ 2023-11-27 726/week @ 2023-12-04 681/week @ 2023-12-11 777/week @ 2023-12-18 465/week @ 2023-12-25 466/week @ 2024-01-01 553/week @ 2024-01-08 541/week @ 2024-01-15 636/week @ 2024-01-22 685/week @ 2024-01-29 742/week @ 2024-02-05 842/week @ 2024-02-12 760/week @ 2024-02-19 857/week @ 2024-02-26 980/week @ 2024-03-04

3,511 downloads per month
Used in 12 crates

MIT license

190KB
4K SLoC

Build coverage status crate docs.rs

expectrl

Expectrl is a tool for automating terminal applications.

Expectrl is a rust module for spawning child applications and controlling them and responding to expected patterns in process's output. Expectrl works like Don Libes' Expect. Expectrl allows your script to spawn a child application and control it as if a human were typing commands.

Using the library you can:

  • Spawn process
  • Control process
  • Interact with process's IO(input/output).

expectrl like original expect may shine when you're working with interactive applications. If your application is not interactive you may not find the library the best choise.

Usage

Add expectrl to your Cargo.toml.

# Cargo.toml
[dependencies]
expectrl = "0.7"

An example where the program simulates a used interacting with ftp.

use expectrl::{spawn, Regex, Eof, Error};

fn main() -> Result<(), Error> {
    let mut p = spawn("ftp speedtest.tele2.net")?;
    p.expect(Regex("Name \\(.*\\):"))?;
    p.send_line("anonymous")?;
    p.expect("Password")?;
    p.send_line("test")?;
    p.expect("ftp>")?;
    p.send_line("cd upload")?;
    p.expect("successfully changed.\r\nftp>")?;
    p.send_line("pwd")?;
    p.expect(Regex("[0-9]+ \"/upload\""))?;
    p.send_line("exit")?;
    p.expect(Eof)?;
    Ok(())
}

The same example but the password will be read from stdin.

use std::io::stdout;
use expectrl::{
    interact::{actions::lookup::Lookup, InteractOptions},
    spawn, stream::stdin::Stdin,
    ControlCode, Error, Regex,
};

fn main() -> Result<(), Error> {
    let mut auth = false;
    let mut login_lookup = Lookup::new();
    let opts = InteractOptions::new(&mut auth).on_output(|ctx| {
        if login_lookup
            .on(ctx.buf, ctx.eof, "Login successful")?
            .is_some()
        {
            **ctx.state = true;
            return Ok(true);
        }

        Ok(false)
    });

    let mut p = spawn("ftp bks4-speedtest-1.tele2.net")?;

    let mut stdin = Stdin::open()?;
    p.interact(&mut stdin, stdout()).spawn(opts)?;
    stdin.close()?;

    if !auth {
        println!("An authefication was not passed");
        return Ok(());
    }

    p.expect("ftp>")?;
    p.send_line("cd upload")?;
    p.expect("successfully changed.")?;
    p.send_line("pwd")?;
    p.expect(Regex("[0-9]+ \"/upload\""))?;
    p.send(ControlCode::EndOfTransmission)?;
    p.expect("Goodbye.")?;
    Ok(())
}

For more examples, check the examples directory.

Features

  • It has an async support (To enable them you must turn on an async feature).
  • It supports logging.
  • It supports interact function.
  • It works on windows.

Notes

It was originally inspired by philippkeller/rexpect and pexpect.

Licensed under MIT License

Dependencies

~4–37MB
~524K SLoC