Connection Factory Methods
connectWithPassword
Connects to an SSH server using password authentication.
static connectWithPassword(
host: string,
port: number,
username: string,
password: string,
callback?: CallbackFunction<SSHClient>
): Promise<SSHClient>
Parameters
The hostname or IP address of the SSH server.
The port number of the SSH server (typically 22).
The username for authentication.
The password for authentication.
callback
CallbackFunction<SSHClient>
Optional callback function to handle any errors during the connection process.
Returns
A Promise that resolves to an instance of SSHClient if the connection is successful.
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
try {
const client = await SSHClient.connectWithPassword(
'example.com',
22,
'username',
'password'
);
console.log('Connected successfully');
} catch (error) {
console.error('Connection failed:', error);
}
connectWithKey
Connects to an SSH server using a private key for authentication.
static connectWithKey(
host: string,
port: number,
username: string,
privateKey: string,
passphrase?: string,
callback?: CallbackFunction<SSHClient>
): Promise<SSHClient>
Parameters
The hostname or IP address of the SSH server.
The port number of the SSH server (typically 22).
The username for authentication.
The private key for authentication in PEM format.
The passphrase for the private key (optional). Required if the private key is encrypted.
callback
CallbackFunction<SSHClient>
A callback function to handle the connection result (optional).
Returns
A Promise that resolves to an instance of SSHClient if the connection is successful. Otherwise, it rejects with an error.
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`;
try {
const client = await SSHClient.connectWithKey(
'example.com',
22,
'username',
privateKey,
'passphrase' // Optional
);
console.log('Connected successfully');
} catch (error) {
console.error('Connection failed:', error);
}
Key Management Methods
generateKeyPair
Generates a new SSH key pair for authentication.
static generateKeyPair(
type: string,
passphrase?: string,
keySize?: number,
comment?: string
): Promise<genKeyPair>
Parameters
The type of key to generate (e.g., ‘rsa’, ‘ed25519’, ‘ecdsa’).
Optional passphrase to encrypt the private key.
Optional key size in bits (e.g., 2048, 4096 for RSA). Defaults vary by key type.
Optional comment to include with the key pair.
Returns
A Promise that resolves to an object containing the generated key pair.interface genKeyPair {
privateKey: string;
publicKey?: string;
}
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
try {
const keyPair = await SSHClient.generateKeyPair(
'rsa',
'my-passphrase',
4096,
'user@example.com'
);
console.log('Private Key:', keyPair.privateKey);
console.log('Public Key:', keyPair.publicKey);
} catch (error) {
console.error('Key generation failed:', error);
}
getKeyDetails
Retrieves the details of an SSH private key.
static getKeyDetails(
key: string
): Promise<{ keyType: string; keySize: number }>
Parameters
The SSH private key as a string in PEM format.
Returns
Promise<keyDetail>
Promise<{ keyType: string; keySize: number }>
A Promise that resolves to the details of the key.Properties:
keyType (string): The type of the key (e.g., ‘RSA’, ‘ED25519’, ‘ECDSA’)
keySize (number): The size of the key in bits
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`;
try {
const details = await SSHClient.getKeyDetails(privateKey);
console.log('Key Type:', details.keyType);
console.log('Key Size:', details.keySize);
} catch (error) {
console.error('Failed to get key details:', error);
}
Types
CallbackFunction
type CallbackFunction<T> = (error: any, response?: T) => void
Represents a callback function with an optional response.
genKeyPair
interface genKeyPair {
privateKey: string;
publicKey?: string;
}
Represents the result of generating a key pair.
keyDetail
interface keyDetail {
keyType: string;
keySize?: number;
}
Represents the details of an SSH key.