UNPKG

ssh2-connect

Version:

Callback-based api behind ssh2 to open an SSH connection

75 lines (72 loc) 3.14 kB
import { ConnectConfig as ConnectConfig$1, Client } from 'ssh2'; type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ? `${T extends Capitalize<T> ? "_" : ""}${Lowercase<T>}${CamelToSnakeCase<U>}` : S; type KeysToSnakeCase<T> = { [K in keyof T as CamelToSnakeCase<string & K>]: T[K]; }; interface ConnectConfig extends ConnectConfig$1, KeysToSnakeCase<ConnectConfig$1> { retry?: number | boolean; wait?: number; privateKey?: string | Buffer; private_key?: string | Buffer; privateKeyPath?: string | boolean; private_key_path?: string | boolean; } /** * Establishes an SSH connection using the provided configuration options. * * @param options - The configuration options for the SSH connection. * @param options.username - The username for authentication. Defaults to the current user if not provided. * @param options.retry - The number of connection retry attempts. Set to `0` or `false` to disable retries, default is `1`. * @param options.wait - The wait time in milliseconds between each attempts, default to `500`. * @param options.privateKey - The private key as a string or Buffer for authentication. * @param options.privateKeyPath - The path to the private key file, or true for auto-discovery in ~/.ssh. * @param options.password - The password for authentication. * @param options.[key: string] - Any other valid SSH2 connection options. * * @returns A Promise that resolves to an SSH2 Client instance when the connection is established. * * @throws Will reject the promise with an error if the connection fails after all retry attempts. * * @example * ```typescript * const client = await connect({ * host: 'example.com', * username: 'user', * privateKeyPath: '~/.ssh/id_ed25519' * }); * ``` */ declare const connect: (options: ConnectConfig) => Promise<Client>; /** * Close the the SSH client connection. * * @param conn - The SSH client connection to close. * * @returns A boolean value indicating whether the connection was closed (true) or if it was already closed (false). */ declare const close: (conn: Client) => PromiseLike<boolean>; /** * Checks if the provided argument `conn` is an instance of the `Client` connection class from the ssh2 package. * * @param conn - The object to check, probably an SSH client connection. * * @returns A boolean value indicating whether the given object is an SSH client connection or not. */ declare const is: (conn: unknown) => boolean; /** * Checks if the provided SSH client connection is closed. * * @param conn - The SSH client connection to check. * * @returns A boolean value indicating whether the connection is closed (true) or open (false). */ declare const closed: (conn: Client) => boolean; /** * Checks if the provided SSH client connection is open and writable. * * @param conn - The SSH client connection to check. * * @returns A boolean value indicating whether the connection is open and writable (true) or closed (false). */ declare const opened: (conn: Client) => boolean; export { type ConnectConfig, close, closed, connect, connect as default, is, opened };