open ssh client wrapper library for nodejs
npm install ssh-client-wrapper
Here is a simple example of how to use ssh-client-wrapper
to execute a command on a remote server.
import SshClientWrapper from 'ssh-client-wrapper';
const hostInfo = {
host: 'remote.server.com',
port: 22,
user: 'username',
password: 'password'
};
const ssh = new SshClientWrapper(hostInfo);
async function run() {
try {
await ssh.canConnect();
console.log('Connection successful!');
const { output, rt } = await ssh.execAndGetOutput('ls -l /home');
if (rt === 0) {
console.log('Directory listing:');
output.forEach(line => console.log(line));
} else {
console.error('Error executing command');
}
} catch (err) {
console.error('Connection or command failed:', err);
} finally {
ssh.disconnect();
}
}
run();
Creates a new SSH client instance.
hostInfo
(Object): Connection details for the remote host.host
(string): The hostname or IP address of the server.user
(string): The username for authentication.port
(number, optional): The port number. Defaults to 22.password
(string | Function, optional): The password for authentication, or a function that returns the password.keyFile
(string, optional): The path to the private key file for key-based authentication.passphrase
(string | Function, optional): The passphrase for the private key, or a function that returns it.noStrictHostKeyChecking
(boolean, optional): Iftrue
, bypasses strict host key checking.- And more... see
lib/index.js
for all available options.
Executes a command on the remote host.
- Returns:
Promise<number>
- The return code of the command.
Executes a command and returns its standard output.
- Returns:
Promise<{output: string[], rt: number}>
- An object containing the output as an array of strings and the return code.
Executes the ls
command on the remote host.
- Returns:
Promise<string[]>
- The output of thels
command as an array of strings.
Executes a command and interacts with it, similar to the expect
tool.
- Returns:
Promise<number>
- The return code of the command.
Uploads files or directories to the remote host using rsync
.
src
(string[]): An array of local paths to send.dst
(string): The remote destination path.- Returns:
Promise<void>
Downloads files or directories from the remote host using rsync
.
src
(string[]): An array of remote paths to receive.dst
(string): The local destination path.- Returns:
Promise<void>
Checks if a connection to the remote host can be established.
- Returns:
Promise<boolean>
- Resolves withtrue
on success.
Closes the master SSH connection to the remote host.