ssh/channel/backend/
channel_exec.rs1use super::channel::ChannelBroker;
2use crate::error::SshResult;
3use crate::model::Data;
4use crate::{
5 constant::{ssh_connection_code, ssh_str},
6 SshError,
7};
8use std::ops::{Deref, DerefMut};
9
10pub struct ExecBroker {
11 channel: ChannelBroker,
12 command_send: bool,
13}
14
15impl ExecBroker {
16 pub(crate) fn open(channel: ChannelBroker) -> Self {
17 Self {
18 channel,
19 command_send: false,
20 }
21 }
22
23 pub fn send_command(&mut self, command: &str) -> SshResult<()> {
28 if self.command_send {
29 return Err(SshError::GeneralError(
30 "An exec channle can only send one command".to_owned(),
31 ));
32 }
33
34 tracing::debug!("Send command {}", command);
35 self.command_send = true;
36 let mut data = Data::new();
37 data.put_u8(ssh_connection_code::CHANNEL_REQUEST)
38 .put_u32(self.server_channel_no)
39 .put_str(ssh_str::EXEC)
40 .put_u8(true as u8)
41 .put_str(command);
42 self.send(data)
43 }
44
45 pub fn get_result(&mut self) -> SshResult<Vec<u8>> {
50 self.recv_to_end()
51 }
52}
53
54impl Deref for ExecBroker {
55 type Target = ChannelBroker;
56 fn deref(&self) -> &Self::Target {
57 &self.channel
58 }
59}
60
61impl DerefMut for ExecBroker {
62 fn deref_mut(&mut self) -> &mut Self::Target {
63 &mut self.channel
64 }
65}