SshClient

High-level async SSH client API.

This is the main entry point for establishing SSH connections. All methods are suspend functions for use with Kotlin coroutines.

Usage with TCP (default):

val client = SshClient("example.com", hostKeyVerifier = myVerifier)
if (client.connect() is ConnectResult.Success) {
if (client.authenticatePassword("user", "password") is AuthResult.Success) {
val session = client.openSession()
session?.requestPty()
session?.requestShell()
// read/write
session?.close()
}
client.disconnect()
}

Usage with custom transport:

val config = SshClientConfig {
transportFactory = MyCustomTransportFactory()
}
val client = SshClient(config)
client.connect()
// ...

For blocking Java compatibility, use org.connectbot.sshlib.blocking.BlockingSshClient.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Negotiated algorithm details for the current connection.

Link copied to clipboard
val disconnectedFlow: SharedFlow<Throwable?>

Emits when the connection drops unexpectedly.

Link copied to clipboard

Check if connected and authenticated.

Functions

Link copied to clipboard
suspend fun authenticate(username: String, handler: AuthHandler): AuthResult

Authenticate using the strategy-based AuthHandler flow.

Link copied to clipboard

Authenticate using keyboard-interactive authentication (RFC 4256).

Link copied to clipboard
suspend fun authenticatePassword(username: String, password: String): AuthResult

Authenticate using password authentication.

Link copied to clipboard
suspend fun authenticatePublicKey(username: String, privateKeyData: ByteArray, passphrase: String? = null): AuthResult
suspend fun authenticatePublicKey(username: String, privateKeyData: String, passphrase: String? = null): AuthResult

Authenticate using public key authentication (RFC 4252 ยง7).

Link copied to clipboard
suspend fun connect(): ConnectResult

Connect to the SSH server and perform key exchange.

Link copied to clipboard
suspend fun disconnect()

Disconnect from the SSH server.

Link copied to clipboard
suspend fun dynamicPortForward(bindAddress: InetSocketAddress, authenticator: Socks5Authenticator? = null): PortForwarder?

Start dynamic (SOCKS5) port forwarding.

suspend fun dynamicPortForward(bindPort: Int, authenticator: Socks5Authenticator? = null): PortForwarder?

Start dynamic (SOCKS5) port forwarding bound to localhost.

Link copied to clipboard

Enable SSH agent forwarding with the provided agent.

Link copied to clipboard
suspend fun forwardStream(readChannel: ByteReadChannel, writeChannel: ByteWriteChannel, remoteHost: String, remotePort: Int, originAddr: String = LOCALHOST, originPort: Int = 0): StreamForwarder?

Forward a pair of streams through an SSH direct-tcpip channel.

Link copied to clipboard
fun isPrivateKeyEncrypted(privateKeyData: String): Boolean

Check if the provided private key data is encrypted and requires a passphrase.

Link copied to clipboard
suspend fun localPortForward(bindAddress: InetSocketAddress, remoteHost: String, remotePort: Int): PortForwarder?

Start local port forwarding (RFC 4254 section 7.2).

suspend fun localPortForward(bindPort: Int, remoteHost: String, remotePort: Int): PortForwarder?

Start local port forwarding bound to localhost.

Link copied to clipboard
fun openDirectTcpipTransport(remoteHost: String, remotePort: Int, originAddr: String = LOCALHOST, originPort: Int = 0): TransportFactory?

Create a TransportFactory that tunnels through this SSH connection.

Link copied to clipboard
suspend fun openSession(): SshSession?

Open a session channel (RFC 4254 section 6.1).

Link copied to clipboard

Open an SFTP session for file transfer.

Link copied to clipboard
suspend fun ping(): PingResult

Send an SSH ping to the server and return the round-trip time.

Link copied to clipboard
suspend fun remotePortForward(remoteBindAddress: String, remoteBindPort: Int, localHost: String, localPort: Int): PortForwarder?

Start remote port forwarding (RFC 4254 section 7.1).