All Projects → MCluck90 → Simple Ssh

MCluck90 / Simple Ssh

Licence: mit
A simple wrapper for Brian White's ssh2 module to make it easier to perform sequential commands.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Simple Ssh

Grunt Sftp Deploy
Grunt task for code deployment over sftp
Stars: ✭ 158 (-9.71%)
Mutual labels:  ssh
Vscode Remote Release
Visual Studio Code Remote Development: Open any folder in WSL, in a Docker container, or on a remote machine using SSH and take advantage of VS Code's full feature set.
Stars: ✭ 2,256 (+1189.14%)
Mutual labels:  ssh
Yubikey Touch Detector
A tool to detect when your YubiKey is waiting for a touch (to send notification or display a visual indicator on the screen)
Stars: ✭ 167 (-4.57%)
Mutual labels:  ssh
Eternalterminal
Re-Connectable secure remote shell
Stars: ✭ 2,191 (+1152%)
Mutual labels:  ssh
Shop
基于SpringMVC,Spring,Hibernate的网上商城。代码已久不维护...
Stars: ✭ 162 (-7.43%)
Mutual labels:  ssh
Fastmac
Get a MacOS or Linux shell, for free, in around 2 minutes
Stars: ✭ 1,953 (+1016%)
Mutual labels:  ssh
Capistrano
Remote multi-server automation tool
Stars: ✭ 12,035 (+6777.14%)
Mutual labels:  ssh
Dracut Crypt Ssh
dracut initramfs module to start dropbear sshd during boot to unlock the root filesystem with the (cryptsetup) LUKS passphrase remotely
Stars: ✭ 171 (-2.29%)
Mutual labels:  ssh
Rtop
rtop is an interactive, remote system monitoring tool based on SSH
Stars: ✭ 1,963 (+1021.71%)
Mutual labels:  ssh
Ssh
Easy SSH servers in Golang
Stars: ✭ 2,254 (+1188%)
Mutual labels:  ssh
Linux Second Screen
Scripts to repurpose old android device as second monitor on linux
Stars: ✭ 160 (-8.57%)
Mutual labels:  ssh
Premotem
Personal Remote Manager
Stars: ✭ 161 (-8%)
Mutual labels:  ssh
Ssh2 Python
Bindings for libssh2 C library.
Stars: ✭ 166 (-5.14%)
Mutual labels:  ssh
Ssh2docker
🐳 standalone SSH server that connects you to your Docker containers
Stars: ✭ 159 (-9.14%)
Mutual labels:  ssh
Psiphon
A multi-functional version of a popular network circumvention tool
Stars: ✭ 169 (-3.43%)
Mutual labels:  ssh
Mirror.vim
Efficient way to edit remote files on multiple environments with Vim.
Stars: ✭ 158 (-9.71%)
Mutual labels:  ssh
Ansible Sshd
Ansible role to configure the OpenSSH server daemon
Stars: ✭ 163 (-6.86%)
Mutual labels:  ssh
Firessh
free, cross-platform SSH terminal client for Firefox and Chrome
Stars: ✭ 173 (-1.14%)
Mutual labels:  ssh
Gitdir
Simple and lightweight SSH git hosting with just a directory.
Stars: ✭ 170 (-2.86%)
Mutual labels:  ssh
Sshautologin
一个使用expect免输入密码自动登录ssh的shell脚本,方便好用,适用Mac、Linux
Stars: ✭ 166 (-5.14%)
Mutual labels:  ssh

Note: I really don't work on this project anymore. It's still fully functional, I'm just not interested in working on it anymore. If you post an issue, I might respond and I might not. If you submit a pull request, I'll most likely accept it. Hopefully it continues to help people around the world. 🌎

simple-ssh

A wrapper for the ssh2 client module by Brian White which makes it easier to run a sequence of commands over SSH.

Requirements

Install

npm install simple-ssh

Examples

  • Echoing out a users PATH:
var SSH = require('simple-ssh');

var ssh = new SSH({
    host: 'localhost',
    user: 'username',
    pass: 'password'
});

ssh.exec('echo $PATH', {
    out: function(stdout) {
        console.log(stdout);
    }
}).start();

/*** Using the `args` options instead ***/
ssh.exec('echo', {
    args: ['$PATH'],
    out: function(stdout) {
        console.log(stdout);
    }
}).start();
  • Connecting with the active SSH Agent with Agent Forwarding
var ssh = new ssh({
    host: 'localhost',
    user: 'username',
    agent: process.env.SSH_AUTH_SOCK,
    agentForward: true
})
  • Capturing error output:
ssh.exec('this-does-not-exist', {
    err: function(stderr) {
        console.log(stderr); // this-does-not-exist: command not found
    }
}).start();
  • Capturing error codes:
ssh.exec('exit 69', {
    exit: function(code) {
        console.log(code); // 69
    }
}).start();
  • Sending data to stdin:
ssh.exec('cat > /path/to/remote/file', {
   in: fs.readFileSync('/path/to/local/file')
}).start();
  • Chaining commands together:
ssh
    .exec('echo "Node.js"', {
        out: console.log.bind(console)
    })
    .exec('echo "is"', {
        out: console.log.bind(console)
    })
    .exec('echo "awesome!"', {
        out: console.log.bind(console)
    })
    .start();

// Output:
// Node.js
// is
// awesome!
  • Get the number of commands:
ssh
    .exec('exit 1')
    .exec('exit 2')
    .exec('exit 3');

console.log(ssh.count()); // 3
  • Running a command using sudo
ssh.exec('sudo echo "Pseudo-sudo"', {
    pty: true,
    out: console.log.bind(console)
}).start();
  • Resetting a connection and the commands
// Echos out any messages the user sent in if 10 or more have been queued
var msgInterval = setInterval(function() {
    if (ssh.count() > 10) {
        ssh.start();
    }
}, 1000);

socket.on('message', function(msg) {
    // If a 'reset' message is received, clear previous messages
    if (msg === 'reset') {
        ssh.reset(function(err) {
            if (err) {
                throw err;
            }

            ssh.exec('echo "reset"');
        });
    } else {
        ssh.exec('echo "' + msg + '"');
    }
});
  • Listening for additional events
ssh.on('error', function(err) {
    console.log('Oops, something went wrong.');
    console.log(err);
    ssh.end();
});
  • Event handlers can be chained as well
ssh
    .on('error', onSSHError)
    .on('ready', onSSHReady);

API

Functions

  • Constructor( [ config ] )
    • config { Object }:
      • config.host { String }: Hostname
      • config.port { Number }: Port number (default: 22)
      • config.user { String }: Username
      • config.pass { String }: Password
      • config.timeout { Number }: Connection timeout in milliseconds (default: 10000)
      • config.key { String }: SSH key
      • config.passphrase { String }: Passphrase
      • config.baseDir { String }: Base directory. If this is set, each command will be preceeded by cd ${this.baseDir}
      • config.agent { String }: Connects with the given SSH agent. If this is set, no need to specify a private key or password.
      • config.agentForward { Boolean }: Set to true to connect with agent forwarding.
  • exec( command, [ options ] ): Adds a command to the stack
    • command { String }: Command to be executed
    • options { Object }:
      • options.args { String[] }: Additional command line arguments (default: null)
      • options.in { String }: Input to be sent to stdin
      • options.out { Function( stdout ) }: stdout handler
        • stdout { String }: Output streamed through stdout
      • options.err { Function( stderr ) }: stderr handler
        • stderr { String }: Output streamed through stderr
      • options.exit { Function( code, stdout, stderr ) }: Exit handler
        • code { Number }: Exit code
        • stdout { String }: All of the standard output concatenated together
        • stderr { String }: All of the error output concatenated together
      • options.pty { Boolean }: Allocates a pseudo-tty, useful for command which require sudo (default: false)
  • on( event, callback ): Add a listener for the specified event (Courtesy of @alexjab)
    • event { String }: Event to listen to
    • callback { Function }: Executed on the event
  • start( [ options ] ): Starts executing the commands
    • options { Object }:
      • options.success { Function() }: Called on successful connection
      • options.fail { Function( err ) }: Called if the connection failed
        • err { Error }: Error information
  • reset( [ callback ] ): Clears the command queue and resets the current connection
    • callback { Function( err ) }: Called when the connection has been successfully reset
      • err { Error }: Error information
  • end(): Ends the SSH session (this is automatically called at the end of a command queue).

Properties

  • host { String }: Host to connect to
  • port { Number }: Port to connect through (default: 22)
  • user { String }: User name
  • pass { String }: Password
  • timeout { Number }: Connection timeout in milliseconds (default: 10000)
  • key { String }: SSH key
  • baseDir { String }: If set, will change directory to baseDir before each command

Flow Control

Sometimes you may find yourself needing to change which commands are executed. The flow can be changed by returning false from an exit handler.

Note: This only works if false is explicitly returned. "Falsy" values are not sufficient (since undefined is implicitly returned and it's "falsy").

  • Ending prematurely:
ssh
    .exec('pwd', {
        exit: function() {
            return false;
        }
    })
    .exec('echo "Not executed"')
    .start();
  • Running a new queue of commands:
ssh
    .exec('exit', {
        args: [ Math.round(Math.random()) ],
        exit: function(code) {
            if (code === 1) {
                // Setup the new command queue
                ssh.exec('echo "new queue"');
                return false;
            }
        }
    })
    .exec('exit 0', {
        exit: function() {
            console.log('Previous command did not return false');
        }
    })
    .start();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].