File: powershell.rs

package info (click to toggle)
rust-expectrl 0.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: python: 55; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 3,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#[cfg(windows)]
fn main() {
    use expectrl::{repl::spawn_powershell, ControlCode, Regex};

    #[cfg(feature = "async")]
    {
        futures_lite::future::block_on(async {
            let mut p = spawn_powershell().await.unwrap();

            eprintln!("Current hostname",);

            // case 1: execute
            let hostname = p.execute("hostname").await.unwrap();
            println!(
                "Current hostname: {:?}",
                String::from_utf8(hostname).unwrap()
            );

            // case 2: wait until done, only extract a few infos
            p.send_line("type README.md | Measure-Object -line -word -character")
                .await
                .unwrap();
            let lines = p.expect(Regex("[0-9]+\\s")).await.unwrap();
            let words = p.expect(Regex("[0-9]+\\s")).await.unwrap();
            let bytes = p.expect(Regex("([0-9]+)[^0-9]")).await.unwrap();
            // go sure `wc` is really done
            p.expect_prompt().await.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[1]),
            );

            // case 3: read while program is still executing
            p.send_line("ping 8.8.8.8 -t").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::ETX).await.unwrap();
            p.expect_prompt().await.unwrap();
        });
    }
    #[cfg(not(feature = "async"))]
    {
        let mut p = spawn_powershell().unwrap();

        // case 1: execute
        let hostname = p.execute("hostname").unwrap();
        println!(
            "Current hostname: {:?}",
            String::from_utf8(hostname).unwrap()
        );

        // case 2: wait until done, only extract a few infos
        p.send_line("type README.md | Measure-Object -line -word -character")
            .unwrap();
        let lines = p.expect(Regex("[0-9]+\\s")).unwrap();
        let words = p.expect(Regex("[0-9]+\\s")).unwrap();
        let bytes = p.expect(Regex("([0-9]+)[^0-9]")).unwrap();
        // go sure `wc` is really done
        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[1]),
        );

        // case 3: read while program is still executing
        p.send_line("ping 8.8.8.8 -t").unwrap();
        for _ in 0..5 {
            let duration = p.expect(Regex("[0-9.]+ms")).unwrap();
            println!(
                "Roundtrip time: {}",
                String::from_utf8_lossy(duration.get(0).unwrap())
            );
        }

        p.send(ControlCode::ETX).unwrap();
        p.expect_prompt().unwrap();
    }
}

#[cfg(not(windows))]
fn main() {
    panic!("An example doesn't supported on windows")
}