1#[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 let hostname = p.execute("hostname").unwrap();
13 println!("Current hostname: {:?}", String::from_utf8_lossy(&hostname));
14
15 p.send_line("wc /etc/passwd").unwrap();
17 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(); println!(
24 "/etc/passwd has {} lines, {} words, {} chars",
25 lines, words, chars,
26 );
27
28 p.send_line("ping 8.8.8.8").unwrap(); for _ in 0..5 {
31 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 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(); println!("Current hostname: {hostname:?}"); p.send_line("wc /etc/passwd").await.unwrap();
57 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(); println!(
67 "/etc/passwd has {} lines, {} words, {} chars",
68 lines, words, chars,
69 );
70
71 p.send_line("ping 8.8.8.8").await.unwrap(); for _ in 0..5 {
74 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}