ftp_interact/
ftp_interact.rs

1use expectrl::{
2    interact::actions::lookup::Lookup, spawn, stream::stdin::Stdin, ControlCode, Error, Expect,
3    Regex,
4};
5use std::io::stdout;
6
7#[cfg(not(all(windows, feature = "polling")))]
8#[cfg(not(feature = "async"))]
9fn main() -> Result<(), Error> {
10    let mut p = spawn("ftp bks4-speedtest-1.tele2.net")?;
11
12    let mut auth = false;
13    let mut login_lookup = Lookup::new();
14    let mut stdin = Stdin::open()?;
15
16    p.interact(&mut stdin, stdout())
17        .with_state(&mut auth)
18        .set_output_action(move |ctx| {
19            if login_lookup
20                .on(ctx.buf, ctx.eof, "Login successful")?
21                .is_some()
22            {
23                **ctx.state = true;
24                return Ok(true);
25            }
26
27            Ok(false)
28        })
29        .spawn()?;
30
31    stdin.close()?;
32
33    if !auth {
34        println!("An authefication was not passed");
35        return Ok(());
36    }
37
38    p.expect("ftp>")?;
39    p.send_line("cd upload")?;
40    p.expect("successfully changed.")?;
41    p.send_line("pwd")?;
42    p.expect(Regex("[0-9]+ \"/upload\""))?;
43    p.send(ControlCode::EndOfTransmission)?;
44    p.expect("Goodbye.")?;
45    Ok(())
46}
47
48#[cfg(any(all(windows, feature = "polling"), feature = "async"))]
49fn main() {}