Function spawn

Source
pub fn spawn(
    program: &str,
    timeout_ms: Option<u64>,
) -> Result<PtySession, Error>
Expand description

Start command in background in a pty session (pty fork) and return a struct with writer and buffered reader (for unblocking reads).

#Arguments:

  • program: This is split at spaces and turned into a process::Command if you wish more control over this, use spawn_command
  • timeout: If Some: all exp_* commands time out after x milliseconds, if None: never times out. It’s highly recommended to put a timeout there, as otherwise in case of a problem the program just hangs instead of exiting with an error message indicating where it stopped. For automation 30’000 (30s, the default in pexpect) is a good value.
Examples found in repository?
examples/ftp.rs (line 5)
4fn main() -> Result<(), Error> {
5    let mut p = spawn("ftp speedtest.tele2.net", Some(2000))?;
6    p.exp_regex("Name \\(.*\\):")?;
7    p.send_line("anonymous")?;
8    p.exp_string("Password")?;
9    p.send_line("test")?;
10    p.exp_string("ftp>")?;
11    p.send_line("cd upload")?;
12    p.exp_string("successfully changed.\r\nftp>")?;
13    p.send_line("pwd")?;
14    p.exp_regex("[0-9]+ \"/upload\"")?;
15    p.send_line("exit")?;
16    p.exp_eof()?;
17    Ok(())
18}
More examples
Hide additional examples
examples/repl.rs (line 15)
7fn ed_session() -> Result<PtyReplSession, Error> {
8    let mut ed = PtyReplSession {
9        // for `echo_on` you need to figure that out by trial and error.
10        // For bash and python repl it is false
11        echo_on: false,
12
13        // used for `wait_for_prompt()`
14        prompt: "> ".to_owned(),
15        pty_session: spawn("/bin/ed -p '> '", Some(2000))?,
16        // command which is sent when the instance of this struct is dropped
17        // in the below example this is not needed, but if you don't explicitly
18        // exit a REPL then rexpect tries to send a SIGTERM and depending on the repl
19        // this does not end the repl and would end up in an error
20        quit_command: Some("Q".to_owned()),
21    };
22    ed.wait_for_prompt()?;
23    Ok(ed)
24}
examples/exit_code.rs (line 10)
9fn main() -> Result<(), Error> {
10    let p = spawn("cat /etc/passwd", Some(2000))?;
11    match p.process.wait() {
12        Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat exited with code 0, all good!"),
13        _ => println!("cat exited with code >0, or it was killed"),
14    }
15
16    let mut p = spawn("cat /this/does/not/exist", Some(2000))?;
17    match p.process.wait() {
18        Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat succeeded"),
19        Ok(wait::WaitStatus::Exited(_, c)) => {
20            println!("Cat failed with exit code {c}");
21            println!("Output (stdout and stderr): {}", p.exp_eof()?);
22        }
23        // for other possible return types of wait()
24        // see here: https://tailhook.github.io/rotor/nix/sys/wait/enum.WaitStatus.html
25        _ => println!("cat was probably killed"),
26    }
27
28    Ok(())
29}