1#[cfg(unix)]
4use expectrl::{repl::spawn_bash, ControlCode, 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 lines = p.expect(Regex("[0-9]+")).unwrap();
19 let words = p.expect(Regex("[0-9]+")).unwrap();
20 let bytes = p.expect(Regex("[0-9]+")).unwrap();
21 p.expect_prompt().unwrap(); println!(
23 "/etc/passwd has {} lines, {} words, {} chars",
24 String::from_utf8_lossy(&lines[0]),
25 String::from_utf8_lossy(&words[0]),
26 String::from_utf8_lossy(&bytes[0]),
27 );
28
29 p.send_line("ping 8.8.8.8").unwrap(); for _ in 0..5 {
32 let duration = p.expect(Regex("[0-9. ]+ ms")).unwrap();
34 println!("Roundtrip time: {}", String::from_utf8_lossy(&duration[0]));
35 }
36
37 p.send(ControlCode::EOT).unwrap();
38}
39
40#[cfg(unix)]
41#[cfg(feature = "async")]
42fn main() {
43 use futures_lite::io::AsyncBufReadExt;
44
45 futures_lite::future::block_on(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 lines = p.expect(Regex("[0-9]+")).await.unwrap();
59 let words = p.expect(Regex("[0-9]+")).await.unwrap();
60 let bytes = p.expect(Regex("[0-9]+")).await.unwrap();
61 p.expect_prompt().await.unwrap(); println!(
63 "/etc/passwd has {} lines, {} words, {} chars",
64 String::from_utf8_lossy(lines.get(0).unwrap()),
65 String::from_utf8_lossy(words.get(0).unwrap()),
66 String::from_utf8_lossy(bytes.get(0).unwrap()),
67 );
68
69 p.send_line("ping 8.8.8.8").await.unwrap(); for _ in 0..5 {
72 let duration = p.expect(Regex("[0-9. ]+ ms")).await.unwrap();
74 println!(
75 "Roundtrip time: {}",
76 String::from_utf8_lossy(duration.get(0).unwrap())
77 );
78 }
79
80 p.send(ControlCode::EOT).await.unwrap();
81 })
82}
83
84#[cfg(windows)]
85fn main() {
86 panic!("An example doesn't supported on windows")
87}