Giter Site home page Giter Site logo

promised-ssh's Introduction

promised-ssh

Promise wrapped ssh2

Install

npm install --save promised-ssh

Usage

.connect(options)

Takes a object with options similar to ssh2. This object is passed along to ssh2 connect. It returns a promise of an Connection.

.connectMock(options)

Returns a MockConnection which behaves similarly to Connection except it never tries to connect anywhere and the outcome of commands and the mocked connection is determined by mock-options which can be set with .setMockOptions.

.setMockOptions(options)

Sets the options used to determine the outcome of MockConnection.connect and MockConnection.exec. It takes an object with options. The possible options are:

  • failConnect (boolean) - Tells whether MockConnection.connect should fail or not. Default is false. If this is true it will throw an ConnectionError which is exposed under errors.
  • commands (object) - An object with information about return code, stdout and stderr that a command should give. All three options is optional and have the following defaults: code=0, stdout='', stderr=''. The default for the option is {}
  • throwIfMockNotDefined (boolean) - Throw an error if exec is called on a command that doesn't have a mock output defined

Example

ssh.setMockOptions({
  commands: {
    'cd project && make': {
      stdout: 'make: Nothing to be done for `all`.'
    }
  }
})

.setOfflineMode(true)

Will prevent any real connections from being made, causing an error to be throwin instead. Useful when in test mode to make sure tests don't trigger connections to remote servers.

Connection

Connection.connect()

Returns a promise that resolves a Connection-object when ssh2 opens a connection. this.options is used as connect options in ssh2.

Connection.exec(list_of_commands)

Takes a list of commands to run on the current connection. After the commands have been runned the connection will be closed. It returns a promise of an array: [stdout, stderr].

Example

var ssh = require('promised-ssh');

ssh
  .connect({
    host: 'localhost',
    username: 'rolf',
    privateKey: '...'
  })
  .then(function(connection) {
    return connection.exec(['ls -al']);
  })
  .spread(function(stdout, stderr) {
    console.log('Returned with return code ' + return_code);
    if (stdout) console.log('STDOUT: ' + stdout);
    if (stderr) console.log('STDERR: ' + stderr);
  })
  .catch(function(error) {
    // error is here an instance of ssh.errors.CommandExecutionError
    // it contains information about exit code, stdout and stderr
    throw error;
  });

promised-ssh's People

Contributors

coolbit-in avatar ekmartin avatar mcculloughsean avatar relekang avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.