npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ssh2-exec

v0.7.6

Published

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

Downloads

4,341

Readme

Build Status

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 new BSD 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:

connect = require('ssh2-connect');
exec = require('ssh2-exec');
connect({host: localhost}, function(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:

connect = require('ssh2-connect');
exec = require('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

The test suite is run online with Travis.

Contributors