What is node-ssh?
The node-ssh npm package is a lightweight wrapper for SSH2, providing a simple and easy-to-use API for performing SSH operations. It allows you to connect to remote servers, execute commands, transfer files, and manage SSH connections programmatically.
What are node-ssh's main functionalities?
Connecting to a remote server
This feature allows you to establish an SSH connection to a remote server using the provided host, username, and private key.
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH();
ssh.connect({
host: 'example.com',
username: 'user',
privateKey: '/path/to/private/key'
}).then(() => {
console.log('Connected to the server');
}).catch(err => {
console.error('Error connecting to the server:', err);
});
Executing commands on a remote server
This feature allows you to execute shell commands on the connected remote server and retrieve the output.
ssh.execCommand('ls -la', { cwd: '/home/user' }).then(result => {
console.log('STDOUT: ' + result.stdout);
console.log('STDERR: ' + result.stderr);
}).catch(err => {
console.error('Error executing command:', err);
});
Transferring files to a remote server
This feature allows you to transfer files from your local machine to the remote server.
ssh.putFile('/local/path/to/file.txt', '/remote/path/to/file.txt').then(() => {
console.log('File transferred successfully');
}).catch(err => {
console.error('Error transferring file:', err);
});
Transferring files from a remote server
This feature allows you to download files from the remote server to your local machine.
ssh.getFile('/local/path/to/file.txt', '/remote/path/to/file.txt').then(() => {
console.log('File downloaded successfully');
}).catch(err => {
console.error('Error downloading file:', err);
});
Other packages similar to node-ssh
ssh2
The ssh2 package is a more low-level SSH client for Node.js, providing a comprehensive set of features for SSH connections, including command execution, file transfers, and tunneling. It offers more control and customization options compared to node-ssh but requires more boilerplate code.
simple-ssh
The simple-ssh package is another lightweight SSH client for Node.js, focusing on simplicity and ease of use. It provides basic SSH functionalities like command execution and file transfers but lacks some of the advanced features and flexibility offered by node-ssh.
ssh2-sftp-client
The ssh2-sftp-client package is a wrapper around the ssh2 package, specifically designed for SFTP operations. It simplifies file transfer operations over SFTP, making it easier to use for file management tasks compared to node-ssh, which provides a broader range of SSH functionalities.
Node-SSH - SSH2 with Promises
Node-SSH is an extremely lightweight Promise wrapper for ssh2, Period.
Example
var path, node_ssh, ssh, fs
fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
ssh.connect({
host: 'localhost',
username: 'steel',
privateKey: '/home/steel/.ssh/id_rsa'
})
.then(function() {
ssh.putFile('/home/steel/Lab/localPath', '/home/steel/Lab/remotePath').then(function() {
console.log("The File thing is done")
}, function(error) {
console.log("Something's wrong")
console.log(error)
})
ssh.putFiles([{ local: '/home/steel/Lab/localPath', remote: '/home/steel/Lab/remotePath' }]).then(function() {
console.log("The File thing is done")
}, function(error) {
console.log("Something's wrong")
console.log(error)
})
ssh.getFile('/home/steel/Lab/localPath', '/home/steel/Lab/remotePath').then(function(Contents) {
console.log("The File's contents were successfully downloaded")
}, function(error) {
console.log("Something's wrong")
console.log(error)
})
const failed = []
const successful = []
ssh.putDirectory('/home/steel/Lab', '/home/steel/Lab', {
recursive: true,
validate: function(itemPath) {
const baseName = path.basename(itemPath)
return baseName.substr(0, 1) !== '.' &&
baseName !== 'node_modules'
},
tick: function(localPath, remotePath, error) {
if (error) {
failed.push(localPath)
} else {
successful.push(localPath)
}
}
}).then(function(status) {
console.log('the directory transfer was', status ? 'successful' : 'unsuccessful')
console.log('failed transfers', failed.join(', '))
console.log('successful transfers', successful.join(', '))
})
ssh.execCommand('hh_client --json', { cwd:'/var/www' }).then(function(result) {
console.log('STDOUT: ' + result.stdout)
console.log('STDERR: ' + result.stderr)
})
ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout', options: { pty: true } }).then(function(result) {
console.log('STDOUT: ' + result)
})
})
API
class SSH{
connect(config: SSH2Config): Promise<this>
requestSFTP(): Promise<SSH2SFTP>
requestShell(): Promise<SSH2Shell>
mkdir(path: string): Promise<string>
exec(command: string, parameters: Array<string>, options: { cwd?: string, options?: Object, stdin?: string, stream?: 'stdout' | 'stderr', 'both' } = {}): Promise<Object | string>
execCommand(command: string, options: { cwd: string, stdin: string } = {}): Promise<{ stdout: string, options?: Object, stderr: string, signal: ?string, code: number }>
putFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise<void>
getFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise<void>
putFiles(files: Array<{ local: string, remote: string }>, sftp: ?Object = null, maxAtOnce: number = 5, opts: ?Object = null): Promise<void>
putDirectory(localDirectory: string, remoteDirectory: string, options: ?{ recursive: boolean, tick(localPath, remotePath, error): any, validate(localPath): boolean } = null, sftp: ?Object = null, opts: ?Object = null): Promise<boolean>
dispose(): void
}
License
This project is licensed under the terms of MIT license. See the LICENSE file for more info.