Code Monkey home page Code Monkey logo

ssh-exec's Introduction

ssh-exec

Execute a script over ssh using Node.JS and pipe to and from it

It is available through npm

npm install ssh-exec

It is written in plain Javascript and uses ssh2 for all the heavy lifting.

Usage

var exec = require('ssh-exec')

// using ~/.ssh/id_rsa as the private key

exec('ls -lh', '[email protected]').pipe(process.stdout)

// or using the more settings

exec('ls -lh', {
  user: 'ubuntu',
  host: 'my-remote.com',
  key: myKeyFileOrBuffer,
  password: 'my-user-password'
}).pipe(process.stdout)

// or if you want to pipe some data to the remote process

process.stdin
  .pipe(exec('echo try typing something; cat -', '[email protected]'))
  .pipe(process.stdout)

Optionally there is a callback api as well

exec('ls -lh', '[email protected]', function (err, stdout, stderr) {
  console.log(err, stdout, stderr)
})

License

MIT

ssh-exec's People

Contributors

andyfleming avatar feross avatar mafintosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ssh-exec's Issues

Add port to opts

My ssh server runs on a non-std port....the opts object needs to pass on these settings to the ssh2 connect call.

How to perform a series of commands?

I'm wondering how I could use this module for running a series of conditional ssh commands. For example:

var folder = "example";
exec('cd ' + folder, '[email protected]', function (err, stdout, stderr) {
  if(stdout.indexOf("No such file or directory") {
    exec("mkdir " + folder, '[email protected]', function (err, stdout, stderr) {

    });
  }
  exec("do something", "[email protected]", function (err,stdout,stderr) {

  });
})

Is this a valid approach? Is it necessary to provide the credentials for every command? Any suggestions?

Exit code not being captured

let sshExec = require('ssh-exec')

sshExec('exit 1', 'user@example', (err, stdout, stderr) => {
  if (err) {
    console.log('error found')
    console.log(err)
  } else {
    console.log('error not found')
  }
})

Outputs error not found.

Can I stream a command? like tail -f file?

Can I stream a command? like tail -f file? I am trying to use this in a web application and I want to stream output of a command to the client? like this. How can I achieve this?

export function execute(req, res) {

  console.log("execute");
  console.log(req.body);
  var device = req.body;

  try{
    exec('tail -f file.txt', {
      user: device.username,
      host: device.ipaddress,
      password: device.password
    }).pipe(res);
  }
  catch(e){
    res.error(e)
  }

}

pipe some data to the remote process

Hey, sorry maybe doing something wrong but can't find anything in the output file, see below:

var exec = require('ssh-exec');
var fs = require('fs');
file = fs.createWriteStream('rep-output.txt');
process.stdin
.pipe(exec(command, host))
.pipe(file);
file.end();

here it is:
$ cat rep-output.txt
$
but because 'command' is 'touch output.txt' I can see it executed correctly on remote 'host':
$ ls -l output.txt
-rw-rw-r-- 1 irekr irekr 0 Jul 4 10:53 output.txt

How to check if exec succeeded or not

Is it possible to do something like this:

exec.on('error' , function(err) {
  //do something
});

In the code I see that duplex emits an error event, how to catch it?

How do you enter your sudo password?

There are some great examples in issue 13 (and it would be useful if they were on the front page.)

But how do you run a sudo command? Can you redirect the ask for password prompt & keyboard input into the the remote server somehow?

Duplexify exception

When running this script (with a fresh install, [email protected]):

var sshExec = require('ssh-exec');

sshExec('ls -l', {
  host : 'xxx.xxx.xxx.xxx',
  user : 'root'
});

I get this exception from duplexify:

/home/unitech/gridcontrol/gridcontrol/grid-cli/node_modules/duplexify/index.js:161
  while ((data = this._readable2.read(state.buffer.length ? state.buffer[0].length : state.length)) !== null) {
                                                                           ^
TypeError: Cannot read property 'length' of undefined
    at Duplexify._forward (/home/unitech/gridcontrol/gridcontrol/grid-cli/node_modules/duplexify/index.js:161:76)
    at Channel.onreadable (/home/unitech/gridcontrol/gridcontrol/grid-cli/node_modules/duplexify/index.js:126:10)
    at emitNone (events.js:86:13)
    at Channel.emit (events.js:185:7)
    at emitReadable_ (_stream_readable.js:433:10)
    at emitReadable (_stream_readable.js:427:7)
    at readableAddChunk (_stream_readable.js:188:13)
    at Channel.Readable.push (_stream_readable.js:135:10)
    at SSH2Stream.<anonymous> (/home/unitech/gridcontrol/gridcontrol/grid-cli/node_modules/ssh2/lib/Channel.js:146:15)
    at emitOne (events.js:96:13)

Add a Callback

Hi,

I'm using this package within a Meteor JS application and it would be a great help if the exec() function could be passed a Callback.

Apologies if this is already possible, I didn't have any luck trying.

Streamed connection doesn't output anything

Going by your example, I tried doing

process.stdin
            .pipe(exec('echo try typing something; cat -', hostInstance))
            .pipe(process.stdout);

It connects, but when I run a command, nothing happens?

Cheers in advance!

How to use a proxy ?

Hey ! I want to know if there is a thing to do to use a proxy with this awesone plugin =D !

I think there is something to do with agents and thing like that but i'm not ultra-skilled ๐Ÿ˜.
Thx.

stdout length

I do an "cat xyz.json" but the stdout is not the full file content. It breaks after 16364 characters...

I use the callback method.

Any ideas why?

Strange behavior on CentOS 7?

$ node -v
v8.9.1

exec('echo superman', '[email protected]', function (err, stdout, stderr) { console.log(err, stdout, stderr) })

Does not echo superman on Centos even with selinux disabled post restart :/
The command still runs, but the client doesn't see the output.

works fine on Debian

Strangely, a git pull does result in some output being send back. Very strange.

Use new version of ssh2

Getting a vulnerability from this package running npm audit, Could ssh-exec be updated to use a more recent version of ssh2?

High OS Command Injection in ssh2
Package ssh
Patched in >=1.4.0
Dependency of ssh-exec
Path ssh-exec > ssh2
More info GHSA-652h-xwhf-q4h6

How to stop and disconnect.

Hi,

I was trying this package to execute command on remote linux server. It is working as per my expectations. All i want to know , how to stop the execution and disconnect ssh connection on demand. I am using below code to get the system stats.

var exec = require('ssh-exec') var v_host = 'XX.XX.XX.XXX' exec('vmstat 1 300', { user: 'root', host: 'XX.XX.XX.XXX', password: 'password' }).pipe(process.stdout , function (err, data) { if ( err ) { console.log(v_host); console.log(err); } console.log(data) })

Please suggest.

How to add the key?

In your example

exec('ls -lh', {
  user: 'ubuntu',
  host: 'my-remote.com',
  key: myKeyFileOrBuffer,
  password: 'my-user-password'
}).pipe(process.stdout)

what type is myKeyFileOrBuffer? is it a path to the key file?

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.