Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

An SSH session handle that provides methods for executing commands, transferring files, and managing interactive shells.

Use Session::init to create a new session builder.

Implementations§

Source§

impl<'sb> Session

Source

pub fn init() -> SessionBuilder<'sb>

Creates a new session builder with default settings.

Defaults:

  • Host: localhost
  • User: root
  • Port: 22
  • Command: bash
  • Inactivity timeout: 3000 seconds
§Example
use simple_ssh::Session;

let session = Session::init()
    .with_host("example.com")
    .with_user("admin")
    .with_passwd("secret")
    .build();
Source

pub async fn connect(self) -> Result<Self>

Connects to the SSH server using the configured authentication method.

§Errors

Returns an error if:

  • The hostname cannot be resolved
  • The connection fails
  • Authentication fails
Source

pub async fn pty(&mut self) -> Result<u32>

Opens an interactive PTY (pseudo-terminal) session.

This is useful for interactive shell sessions where you want terminal emulation support.

§Returns

The exit code of the shell session.

§Errors

Returns an error if no connection is established.

Source

pub fn pty_builder(&mut self) -> PtyBuilder<'_>

Creates a PTY builder for advanced terminal configuration.

The builder provides options for raw mode, terminal type, dimensions, and custom commands.

§Example
let exit_code = session.pty_builder()
    .with_raw()
    .with_term("xterm-256color")
    .run().await?;
Source

pub async fn run(&mut self) -> Result<u32>

Runs the configured command with output to stdout and stderr.

Uses the command specified via SessionBuilder::with_cmd.

§Returns

The exit code of the command.

§Errors

Returns an error if no connection is established.

Source

pub async fn exec(&mut self, command: &Vec<String>) -> Result<u32>

Executes a command with the given arguments.

§Arguments
  • command - A vector of command and its arguments
§Returns

The exit code of the command.

§Errors

Returns an error if no connection is established.

Source

pub async fn system(&mut self, command: &str) -> Result<u32>

Executes a shell command via sh -c.

§Arguments
  • command - The shell command to execute
§Returns

The exit code of the command.

§Errors

Returns an error if no connection is established.

Source

pub async fn cmd(&mut self, command: &str) -> Result<u32>

Executes a single command string.

§Arguments
  • command - The command to execute
§Returns

The exit code of the command.

§Errors

Returns an error if no connection is established.

Source

pub async fn scp(&mut self, from: &str, to: &str) -> Result<()>

Transfers a file to the remote host via SCP.

§Arguments
  • from - Local file path
  • to - Remote destination path
§Errors

Returns an error if:

  • No connection is established
  • The local file cannot be read
  • The transfer fails
Source

pub async fn close(&mut self) -> Result<()>

Closes the SSH session gracefully.

§Errors

Returns an error if the disconnect message fails to send.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V