Skip to main content

Overview

The SSHClient class is the main interface for SSH and SFTP operations in the React Native SSH SFTP library. Instances are created using static factory methods rather than the constructor directly. Instances should be created using:
  • SSHClient.connectWithPassword() - For password authentication
  • SSHClient.connectWithKey() - For private key authentication

Constructor

The constructor is private and should not be called directly. Use the static factory methods instead.
constructor(
  host: string,
  port: number,
  username: string,
  passwordOrKey: PasswordOrKey,
  callback: CallbackFunction<void>
)
Do not instantiate SSHClient directly. Use connectWithPassword() or connectWithKey() static methods.

Static Methods Overview

The SSHClient class provides the following static methods:

Connection Factory Methods

  • connectWithPassword() - Create an SSH connection using password authentication
  • connectWithKey() - Create an SSH connection using private key authentication
For detailed documentation, see Connection Methods.

Key Management Methods

  • generateKeyPair() - Generate a new SSH key pair
  • getKeyDetails() - Retrieve details about an SSH private key
For detailed documentation, see Connection Methods.

Types

PtyType

Represents the types of PTY (pseudo-terminal) for SSH shell connections.
enum PtyType {
  VANILLA = 'vanilla',
  VT100 = 'vt100',
  VT102 = 'vt102',
  VT220 = 'vt220',
  ANSI = 'ansi',
  XTERM = 'xterm',
}

CallbackFunction

Represents a callback function with an optional response.
type CallbackFunction<T> = (error: any, response?: T) => void

EventHandler

Represents an event handler function.
type EventHandler = (value: any) => void

LsResult

Represents the result of a directory listing operation.
interface LsResult {
  filename: string;
  isDirectory: boolean;
  modificationDate: string;
  lastAccess: string;
  fileSize: number;
  ownerUserID: number;
  ownerGroupID: number;
  flags: number;
}

KeyPair

Represents a key pair used for SSH authentication.
interface KeyPair {
  privateKey: string;
  publicKey?: string;
  passphrase?: string;
}

PasswordOrKey

Represents a password or key for authentication.
type PasswordOrKey = string | KeyPair

Instance Methods

Event Handling

on()

Registers an event handler for the specified event. This method is used to listen to asynchronous events from shell sessions and file transfers.
on(eventName: string, handler: EventHandler): void
Parameters:
  • eventName (string) - The name of the event to listen for. Supported events:
    • 'Shell' - Receives output from interactive shell sessions
    • 'UploadProgress' - Receives progress updates during file uploads
    • 'DownloadProgress' - Receives progress updates during file downloads
  • handler (EventHandler) - The function to call when the event occurs
Usage Example:
// Listen for shell output
client.on('Shell', (data) => {
  console.log('Shell:', data);
});

// Listen for upload progress
client.on('UploadProgress', (progress) => {
  console.log('Upload progress:', progress);
});

// Listen for download progress
client.on('DownloadProgress', (progress) => {
  console.log('Download progress:', progress);
});

Other Instance Methods

For documentation on other instance methods, see: