bash/
bash.rs

1// An example is based on README.md from https://github.com/philippkeller/rexpect
2
3#[cfg(unix)]
4use expectrl::{repl::spawn_bash, ControlCode, Expect, Regex};
5
6#[cfg(unix)]
7#[cfg(not(feature = "async"))]
8fn main() {
9    let mut p = spawn_bash().unwrap();
10
11    // case 1: execute
12    let hostname = p.execute("hostname").unwrap();
13    println!("Current hostname: {:?}", String::from_utf8_lossy(&hostname));
14
15    // case 2: wait until done, only extract a few infos
16    p.send_line("wc /etc/passwd").unwrap();
17    // `expect` returns both string-before-match and match itself, discard first
18    let found = p.expect(Regex("([0-9]+).*([0-9]+).*([0-9]+)")).unwrap();
19    let lines = String::from_utf8_lossy(&found[1]);
20    let words = String::from_utf8_lossy(&found[2]);
21    let chars = String::from_utf8_lossy(&found[3]);
22    p.expect_prompt().unwrap(); // go sure `wc` is really done
23    println!(
24        "/etc/passwd has {} lines, {} words, {} chars",
25        lines, words, chars,
26    );
27
28    // case 3: read while program is still executing
29    p.send_line("ping 8.8.8.8").unwrap(); // returns when it sees "bytes of data" in output
30    for _ in 0..5 {
31        // times out if one ping takes longer than 2s
32        let duration = p.expect(Regex("[0-9. ]+ ms")).unwrap();
33        println!("Roundtrip time: {}", String::from_utf8_lossy(&duration[0]));
34    }
35
36    p.send(ControlCode::EOT).unwrap();
37}
38
39#[cfg(unix)]
40#[cfg(feature = "async")]
41fn main() {
42    use expectrl::AsyncExpect;
43    use futures_lite::io::AsyncBufReadExt;
44
45    let f = async {
46        let mut p = spawn_bash().await.unwrap();
47
48        // case 1: wait until program is done
49        p.send_line("hostname").await.unwrap();
50        let mut hostname = String::new();
51        p.read_line(&mut hostname).await.unwrap();
52        p.expect_prompt().await.unwrap(); // go sure `hostname` is really done
53        println!("Current hostname: {hostname:?}"); // it prints some undetermined characters before hostname ...
54
55        // case 2: wait until done, only extract a few infos
56        p.send_line("wc /etc/passwd").await.unwrap();
57        // `expect` returns both string-before-match and match itself, discard first
58        let found = p
59            .expect(Regex("([0-9]+).*([0-9]+).*([0-9]+)"))
60            .await
61            .unwrap();
62        let lines = String::from_utf8_lossy(&found[1]);
63        let words = String::from_utf8_lossy(&found[2]);
64        let chars = String::from_utf8_lossy(&found[3]);
65        p.expect_prompt().await.unwrap(); // go sure `wc` is really done
66        println!(
67            "/etc/passwd has {} lines, {} words, {} chars",
68            lines, words, chars,
69        );
70
71        // case 3: read while program is still executing
72        p.send_line("ping 8.8.8.8").await.unwrap(); // returns when it sees "bytes of data" in output
73        for _ in 0..5 {
74            // times out if one ping takes longer than 2s
75            let duration = p.expect(Regex("[0-9. ]+ ms")).await.unwrap();
76            println!(
77                "Roundtrip time: {}",
78                String::from_utf8_lossy(duration.get(0).unwrap())
79            );
80        }
81
82        p.send(ControlCode::EOT).await.unwrap();
83    };
84
85    futures_lite::future::block_on(f);
86}
87
88#[cfg(windows)]
89fn main() {
90    panic!("An example is not supported on windows")
91}