0
pub fn execute_command(
    address: &String,
    username: &str,
    command: &Command,
    buf: Option<&Vec<u8>>,
) -> anyhow::Result<Vec<u8>> {
    let tcp = TcpStream::connect(address)?;

    let mut sess = Session::new()?;
    sess.set_tcp_stream(tcp);
    sess.handshake()?;
    sess.userauth_agent(&username)?;

    let mut channel = sess.channel_session()?;
    let commands = format!("{:?}", utilities::nixify(command));
    trace!("channel acquired for {}", address);

    channel.exec(&commands)?;
    trace!("executed successfully command for {}", address);

    if let Some(buf) = buf {
        trace!("writing buffer at address {}", address);
        channel.write_all(buf.as_slice())?;
        trace!("wrote buffer at address {}", address);
    }

    let mut channel_output = Vec::new();
    channel.read_to_end(&mut channel_output)?;
    trace!("channel output len: '{}'", channel_output.len());

    channel.wait_close()?;
    trace!("channel exit status: '{}'", channel.exit_status()?);

    Ok(channel_output)
}

The thing I'm executing on the remote machine outputs some binary data serialized using bincode. This code snippet never gets to the last trace "channel output len ..". My guess would be because it never received EOF, but how does one send an EOF to stdout? I tried flushing stdout on the remote but it didn't help.

ditoslav
  • 4,563
  • 10
  • 47
  • 79

0 Answers0