Skip to content
项目

关于

A rust library for controlling interactive programs in a pseudo-terminal

开发工具

这些数字如何统计

可以做什么

  • 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.

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

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 choiсe.

Usage

Add expectrl to your Cargo.toml.

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

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

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

fn main() -> Result<(), Error> {
    let mut p = expectrl::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, InteractSession},
    stream::stdin::Stdin,
    ControlCode, Error, Expect, Regex,
};

fn main() -> Result<(), Error> {
    let mut p = expectrl::spawn("ftp bks4-speedtest-1.tele2.net")?;

    let mut search = Lookup::new();
    let mut stdin = Stdin::open()?;

    let authenticated = {
        let mut session = InteractSession::new(&mut p, &mut stdin, stdout(), false);
        session.set_output_action(move |ctx| {
            let found = search.on(ctx.buf, ctx.eof, "Login successful")?;
            if found.is_some() {
                *ctx.state = true;
                return Ok(true);
            }

            Ok(false)
        });

        session.spawn()?;

        session.into_state()
    };

    stdin.close()?;

    if !authenticated {
        println!("An authentication 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

探索相关项目

加载中…