Struct openssh::Command

source ·
pub struct Command<'s> { /* private fields */ }
Expand description

A remote process builder, providing fine-grained control over how a new remote process should be spawned.

A default configuration can be generated using Session::command(program), where program gives a path to the program to be executed. Additional builder methods allow the configuration to be changed (for example, by adding arguments) prior to spawning. The interface is almost identical to that of std::process::Command.

Command can be reused to spawn multiple remote processes. The builder methods change the command without needing to immediately spawn the process. Similarly, you can call builder methods after spawning a process and then spawn a new process with the modified settings.

§Environment variables and current working directory.

You’ll notice that unlike its std counterpart, Command does not have any methods for setting environment variables or the current working directory for the remote command. This is because the SSH protocol does not support this (at least not in its standard configuration). For more details on this, see the ENVIRONMENT section of ssh(1). To work around this, give env(1) a try. If the remote shell supports it, you can also prefix your command with ["cd", "dir", "&&"] to run the rest of the command in some directory dir.

§Exit status

The ssh command generally forwards the exit status of the remote process. The exception is if a protocol-level error occured, in which case it will return with exit status 255. Since the remote process could also return with exit status 255, we have no reliable way to distinguish between remote errors and errors from ssh, but this library assumes that 255 means the error came from ssh, and acts accordingly.

Implementations§

source§

impl<'s> Command<'s>

source

pub fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self

Adds an argument to pass to the remote program.

Before it is passed to the remote host, arg is escaped so that special characters aren’t evaluated by the remote shell. If you do not want this behavior, use raw_arg.

Only one argument can be passed per use. So instead of:

.arg("-C /path/to/repo")

usage would be:

.arg("-C")
.arg("/path/to/repo")

To pass multiple arguments see args.

source

pub fn raw_arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self

Adds an argument to pass to the remote program.

Unlike arg, this method does not shell-escape arg. The argument is passed as written to ssh, which will pass it again as an argument to the remote shell. Since the remote shell may do argument parsing, characters such as spaces and * may be interpreted by the remote shell.

To pass multiple unescaped arguments see raw_args.

source

pub fn args<I, S>(&mut self, args: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Adds multiple arguments to pass to the remote program.

Before they are passed to the remote host, each argument in args is escaped so that special characters aren’t evaluated by the remote shell. If you do not want this behavior, use raw_args.

To pass a single argument see arg.

source

pub fn raw_args<I, S>(&mut self, args: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Adds multiple arguments to pass to the remote program.

Unlike args, this method does not shell-escape args. The arguments are passed as written to ssh, which will pass them again as arguments to the remote shell. However, since the remote shell may do argument parsing, characters such as spaces and * may be interpreted by the remote shell.

To pass a single argument see raw_arg.

source

pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Configuration for the remote process’s standard input (stdin) handle.

Defaults to inherit when used with spawn or status, and defaults to null when used with output.

source

pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Configuration for the remote process’s standard output (stdout) handle.

Defaults to inherit when used with spawn or status, and defaults to piped when used with output.

source

pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self

Configuration for the remote process’s standard error (stderr) handle.

Defaults to inherit when used with spawn or status, and defaults to piped when used with output.

source

pub async fn spawn(&mut self) -> Result<RemoteChild<'s>, Error>

Executes the remote command without waiting for it, returning a handle to it instead.

By default, stdin, stdout and stderr are inherited.

source

pub async fn output(&mut self) -> Result<Output, Error>

Executes the remote command, waiting for it to finish and collecting all of its output.

By default, stdout and stderr are captured (and used to provide the resulting output) and stdin is set to Stdio::null().

source

pub async fn status(&mut self) -> Result<ExitStatus, Error>

Executes the remote command, waiting for it to finish and collecting its exit status.

By default, stdin, stdout and stderr are inherited.

Trait Implementations§

source§

impl<'s> Debug for Command<'s>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for Command<'s>

§

impl<'s> RefUnwindSafe for Command<'s>

§

impl<'s> Send for Command<'s>

§

impl<'s> Sync for Command<'s>

§

impl<'s> Unpin for Command<'s>

§

impl<'s> UnwindSafe for Command<'s>

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more