ftp_interact/
ftp_interact.rs

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