package
Transparent usage between `child_process.exec` and `ssh2.prototype.exec`
npm install ssh2-execGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
76.4 kB
Includes dependencies.
The Node.js ssh2-exec package extends the ssh2 module to provide transparent usage between the child_process.exec and ssh2.prototype.exec functions. It was originally developped for and is still use by Nikita to run actions both locally and over SSH.
This is OSS and licensed under the MIT license.
npm install ssh2-execssh2-exec module usageThe default module expose an API similar to the native NodeJS API. Its signature is:
exec(sshOrNull, command, [options], [callback])
Or
exec(options, [callback])
Like in the native NodeJS API, the callback is not required in case you wish to work with the returned child stream. The "sshOrNull" and "command" arguments are also facultative because they could be provided respectively as the "ssh" and "command" property of the options object.
Valid options properties are:
sshcommandcwdendenvptycwduidgidSee the ssh2 and ssh2-connect modules on how to create a new SSH connection.
ssh2-exec/promises module usageNote, until version 0.7.3, the module was named ssh2-exec/promise. The promise resolution is still working. However, it will be removed in an upcoming version in favor of ssh2-exec/promises in order to be consistent with the Node.js node:fs/promises module.
The promise module is an alternative to the callback usage. Like with the callback, use it if stdout and stderr are not too large and fit in memory.
const {stdout, stderr, code} = await exec(sshOrNull, command, [options])
Or
const {stdout, stderr, code} = await exec(options)
If the exit code is not 0, the thrown error object contains the stdout, stderr, and code properties.
A command, a configuration object and a callback:
import { connect } from "ssh2-connect";
import { exec } from "ssh2-exec";
connect({ host: localhost }, (err, ssh) => {
exec("ls -la", { ssh: ssh }, (err, stdout, stderr, code) => {
console.info(stdout, stderr, code);
});
});A configuration object with a ssh2 connection and working a the return child object:
import { connect } from "ssh2-connect";
import { exec } from "ssh2-exec";
connect({ host: localhost }, function (err, ssh) {
child = exec(
{
command: "ls -la",
ssh: ssh,
},
function (err, stdout, stderr, code) {
console.info(stdout);
},
);
child.stdout.on("data", function (data) {
console.info(data);
});
child.stderr.on("data", function (data) {
console.error(data);
Tests are executed with mocha. To install it, simple run npm install, it will install mocha and its dependencies in your project "node_modules" directory.
To run the tests:
npm testTests related to environment variables require a specific SSH server configuration (eg in /etc/ssh/sshd_config):
AcceptEnv property determines accepted environment variables and must contain LANG variable.PermitUserEnvironment property must equal yes.Versions are incremented using semantic versioning. To create a new version and publish it to NPM, run:
npm run releaseThe publication is handled by the GitHub action.
The project is sponsored by Adaltas based in Paris, France. Adaltas offers support and consulting on distributed systems, big data and open source.