The SshClient class provides methods to execute commands on a remote host. It handles connection, authentication, and session management automatically via the authenticatedClient lazy value.
Standard Execution
Use exec(command: Command) to run a command. This method returns a Try[CommandResult]. It is suitable for non-interactive commands.
Pseudo-Terminal (PTY) Execution
Use execPTY(command: Command) if the command requires a pseudo-terminal (e.g., interactive programs, tools that expect a TTY like top or vim). This method uses the ptyConfig defined in your HostConfig to allocate the terminal dimensions and modes.
Command Input and Timeouts
- Input: The
Command object contains an input field (an InputStream) used to pipe data to the remote command's standard input. - Timeout: The command will wait for the duration specified in
command.timeout. If that is not provided, it falls back to config.commandTimeout from the HostConfig.
// Example usage pattern
val client = SshClient("my-remote-host")
val cmd = Command("ls -la", input = new java.io.ByteArrayInputStream(b""))
val result = client.exec(cmd)
result match {
case Success(res) => println(s"Output: ${res.output}")
case Failure(err) => println(s"Error: $err")
}