Package detail

ssh2-exec

adaltas4.2kMIT0.8.4

Transparent usage between child_process.exec and ssh2.prototype.exec

child process, exec, ssh2, ssh

readme

Build Status NPM NPM

Node.js ssh2-exec

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.

Installation

This is OSS and licensed under the MIT license.

npm install ssh2-exec

ssh2-exec module usage

The 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:

  • ssh
    SSH connection if the command must run remotely
  • command
    Command to run unless provided as first argument
  • cwd
    Current working directory
  • end
    Close the SSH connection on exit, default to true if an ssh connection instance is provided.
  • env
    An environment to use for the execution of the command.
  • pty
    Set to true to allocate a pseudo-tty with defaults, or an object containing specific pseudo-tty settings. Apply only to SSH remote commands.
  • cwd
    Apply only to local commands.
  • uid
    Apply only to local commands.
  • gid
    Apply only to local commands.

See the ssh2 and ssh2-connect modules on how to create a new SSH connection.

ssh2-exec/promises module usage

Note, 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.

Examples

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);
  });
  child.on("exit", function (code) {
    console.info("Exit", code);
  });
});

Development

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 test

Release

Versions are incremented using semantic versioning. To create a new version and publish it to NPM, run:

npm run release

The publication is handled by the GitHub action.

Contributors

The project is sponsored by Adaltas based in Paris, France. Adaltas offers support and consulting on distributed systems, big data and open source.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.8.4 (2024-10-24)

Features

0.8.3 (2024-10-24)

0.8.2 (2024-10-17)

0.8.1 (2024-09-29)

Features

  • typescript support and convertion (c64d07a)

0.8.0 (2024-09-23)

⚠ BREAKING CHANGES

  • convert to esm

Bug Fixes

0.7.7 (2024-06-12)

0.7.6 (2023-11-19)

Bug Fixes

0.7.5 (2023-11-19)

0.7.4 (2023-11-19)

0.7.3 (2022-03-11)

0.7.2 (2022-01-12)

Features

  • exit code as third argument (278e962)
  • promise api (5b2da43)
  • use the export package.json field (88b6301)

0.7.1 (2022-01-12)

Features

  • exit code as third argument (278e962)
  • promise api (5b2da43)
  • use the export package.json field (88b6301)

Version 0.7.0

  • feat: support stdio option

Version 0.6.0

  • test: travis with latest node.js
  • api: rename cmd to command

Version 0.5.3

  • package: latest dependencies

Version 0.5.2

  • project: remove lock file

Version 0.5.1

  • project: latest dependencies

Version 0.5.0

  • api: accept multiple options

Version 0.4.2

  • package: improve release commands

Version 0.4.1

  • package: release commands

Version 0.4.0

  • package: update to CoffeeScript 2
  • test: update mocha
  • error: some err message in callback

Version 0.3.5

  • package: latest dependencies
  • readme: use travis svg
  • remote: emit close event #6
  • local: use spawn if no callback #5
  • src: reformat doc
  • package: move ownership
  • readme: update travis url

Version 0.3.4

  • src: stream and connection error
  • package: latest dependencies
  • remote: better error message

Version 0.3.3

  • package: latest dependecies
  • package: use tild
  • readme: fix exemple #4

Version 0.3.2

  • package: latest dependencies
  • package: update license
  • api: remove comments

Version 0.3.1

  • exec: pass x11 option

Version 0.3.0

  • package: latest dependencies

Version 0.2.9

  • no longer check ssh2 instance
  • fix type in readme #3