#[cfg(unix)]
use expectrl::{repl::spawn_bash, ControlCode, Regex};
#[cfg(unix)]
#[cfg(not(feature = "async"))]
fn main() {
let mut p = spawn_bash().unwrap();
let hostname = p.execute("hostname").unwrap();
println!("Current hostname: {:?}", String::from_utf8_lossy(&hostname));
p.send_line("wc /etc/passwd").unwrap();
let lines = p.expect(Regex("[0-9]+")).unwrap();
let words = p.expect(Regex("[0-9]+")).unwrap();
let bytes = p.expect(Regex("[0-9]+")).unwrap();
p.expect_prompt().unwrap(); println!(
"/etc/passwd has {} lines, {} words, {} chars",
String::from_utf8_lossy(&lines[0]),
String::from_utf8_lossy(&words[0]),
String::from_utf8_lossy(&bytes[0]),
);
p.send_line("ping 8.8.8.8").unwrap(); for _ in 0..5 {
let duration = p.expect(Regex("[0-9. ]+ ms")).unwrap();
println!("Roundtrip time: {}", String::from_utf8_lossy(&duration[0]));
}
p.send(ControlCode::EOT).unwrap();
}
#[cfg(unix)]
#[cfg(feature = "async")]
fn main() {
use futures_lite::io::AsyncBufReadExt;
futures_lite::future::block_on(async {
let mut p = spawn_bash().await.unwrap();
p.send_line("hostname").await.unwrap();
let mut hostname = String::new();
p.read_line(&mut hostname).await.unwrap();
p.expect_prompt().await.unwrap(); println!("Current hostname: {hostname:?}"); p.send_line("wc /etc/passwd").await.unwrap();
let lines = p.expect(Regex("[0-9]+")).await.unwrap();
let words = p.expect(Regex("[0-9]+")).await.unwrap();
let bytes = p.expect(Regex("[0-9]+")).await.unwrap();
p.expect_prompt().await.unwrap(); println!(
"/etc/passwd has {} lines, {} words, {} chars",
String::from_utf8_lossy(lines.get(0).unwrap()),
String::from_utf8_lossy(words.get(0).unwrap()),
String::from_utf8_lossy(bytes.get(0).unwrap()),
);
p.send_line("ping 8.8.8.8").await.unwrap(); for _ in 0..5 {
let duration = p.expect(Regex("[0-9. ]+ ms")).await.unwrap();
println!(
"Roundtrip time: {}",
String::from_utf8_lossy(duration.get(0).unwrap())
);
}
p.send(ControlCode::EOT).await.unwrap();
})
}
#[cfg(windows)]
fn main() {
panic!("An example doesn't supported on windows")
}