pub struct SshConnectionManager { /* private fields */ }Expand description
SSH Connection Manager
Manages a persistent SSH connection with the following features:
- Automatic reconnection when connection drops
- Concurrent access protection via mutex/atomic flags
- Optional
suelevation for privileged operations - 30-second connection timeout
Implementations§
Source§impl SshConnectionManager
impl SshConnectionManager
Sourcepub async fn exec_command(
&self,
command: &str,
timeout_duration: Duration,
) -> Result<CommandOutput>
pub async fn exec_command( &self, command: &str, timeout_duration: Duration, ) -> Result<CommandOutput>
Execute a command over SSH
This method:
- Ensures the connection is active
- If elevated (su shell), uses the PTY shell channel
- Otherwise, opens a new exec channel
- Collects stdout/stderr with timeout
- On timeout, attempts graceful abort via pkill
§Arguments
command- The command to execute (should be pre-sanitized)timeout_duration- Maximum time to wait for command completion
§Returns
Ok(CommandOutput)- Command output with stdout, stderr, and exit codeErr(SshMcpError::Timeout)- If command times outErr(SshMcpError::Connection)- If connection issues occur
Sourcepub async fn exec_raw_streaming<R, W>(
&self,
command: &str,
stdin: Option<&mut R>,
stdout: Option<&mut W>,
timeout_duration: Duration,
) -> Result<TransferRawOutput>
pub async fn exec_raw_streaming<R, W>( &self, command: &str, stdin: Option<&mut R>, stdout: Option<&mut W>, timeout_duration: Duration, ) -> Result<TransferRawOutput>
Execute a command over SSH with binary-safe streaming.
This method is designed for use-cases like file transfer where stdout must be treated as bytes and forwarded to a sink without UTF-8 decoding.
Notes:
- This does not use the interactive su shell.
- Timeouts are enforced locally via tokio timeout.
Sourcepub async fn check_process(
&self,
job_id: &str,
tail_lines: usize,
registry: &JobRegistry,
spooler: &LocalLogSpooler,
) -> Result<ProcessStatus>
pub async fn check_process( &self, job_id: &str, tail_lines: usize, registry: &JobRegistry, spooler: &LocalLogSpooler, ) -> Result<ProcessStatus>
Check the status of a background job by job_id.
Uses kill -0 for process detection (existence/permission check without sending a signal).
This avoids parsing ps output (GNU vs BusyBox differences) and works on common Linux
distributions.
§Arguments
job_id- Job id returned by background exectail_lines- Number of lines to read from log tailregistry- Job registry holding current job state
§Returns
ProcessStatus with running state, exit code, elapsed time, command, and log tail
Source§impl SshConnectionManager
impl SshConnectionManager
Sourcepub async fn new(config: SshConfig) -> Self
pub async fn new(config: SshConfig) -> Self
Create a new SSH Connection Manager
Does not establish connection immediately; call connect() or
ensure_connected() to establish the connection.
Sourcepub async fn connect(&self) -> Result<()>
pub async fn connect(&self) -> Result<()>
Establish SSH connection
If already connected, returns immediately. If another task is currently connecting, waits for that connection attempt to complete.
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if the connection is active
Sourcepub async fn ensure_connected(&self) -> Result<()>
pub async fn ensure_connected(&self) -> Result<()>
Ensure connection is established, reconnecting if necessary
Sourcepub async fn with_session<F, T>(&self, f: F) -> Result<T>
pub async fn with_session<F, T>(&self, f: F) -> Result<T>
Get a reference to the session for operations
Instead of cloning the Handle (which doesn’t implement Clone), we provide methods that work with the session directly.
Sourcepub async fn open_channel(&self) -> Result<Channel<Msg>>
pub async fn open_channel(&self) -> Result<Channel<Msg>>
Open a new session channel
Sourcepub fn is_elevated(&self) -> bool
pub fn is_elevated(&self) -> bool
Check if currently elevated to root via su
Sourcepub fn use_timeout_wrapper(&self) -> bool
pub fn use_timeout_wrapper(&self) -> bool
Check if the timeout command is available on the remote system
Uses cached result after first check. To trigger a new check, the connection must be re-established.
Sourcepub fn disable_timeout_wrapper(&self)
pub fn disable_timeout_wrapper(&self)
Disables timeout wrapper for the rest of this connection lifetime
When called, this sets has_timeout_cmd to false, causing all subsequent
commands to fall back to the tokio timeout + pkill method instead of using
the remote timeout command wrapper.
Sourcepub async fn check_timeout_availability(&self) -> bool
pub async fn check_timeout_availability(&self) -> bool
Detect whether the timeout command is available on the remote system
This performs a one-time detection check by running
sh -c 'command -v timeout'
on the remote system. The result is cached for the lifetime of the
connection.
Returns true if timeout is available, false otherwise.
Sourcepub async fn has_su_channel(&self) -> bool
pub async fn has_su_channel(&self) -> bool
Check if an elevated su channel is available
Sourcepub async fn with_su_channel<F, Fut, T>(&self, f: F) -> Result<T>
pub async fn with_su_channel<F, Fut, T>(&self, f: F) -> Result<T>
Execute a closure with access to the su channel
The closure receives a mutable reference to the Option
Sourcepub async fn ensure_elevated(&self) -> Result<()>
pub async fn ensure_elevated(&self) -> Result<()>
Ensure we have an elevated shell via su
This starts an interactive PTY session, runs su -, sends the password,
and waits for the root prompt (#).
Sourcepub fn get_su_password(&self) -> Option<&str>
pub fn get_su_password(&self) -> Option<&str>
Get the su password if configured
Sourcepub fn get_sudo_password(&self) -> Option<&str>
pub fn get_sudo_password(&self) -> Option<&str>
Get the sudo password if configured
Sourcepub async fn set_su_password(&self, password: Option<String>) -> Result<()>
pub async fn set_su_password(&self, password: Option<String>) -> Result<()>
Set or update the su password
If setting a new password, will attempt to establish elevation. If clearing the password (None), will close any existing su shell.
Sourcepub async fn invalidate_session(&self, reason: &str)
pub async fn invalidate_session(&self, reason: &str)
Invalidate the current session and clear elevation state
This clears the session handle, su_channel, and resets elevation state. Used when a connection is detected as broken and needs reconnection.