shell commands via ssh2 nodejs

I’m trying to do basic stuff with ssh2(https://www.npmjs.org/package/ssh2) but I don’t get any result and the api ain’t that understandable (for me), I’m trying to do basic shell commands like ls,pwd .. but ive no results. I tried this to get ls -lah via shell,

c.on('ready', function() {

    c.shell('ls','lah', function(err,stream) {
    if (err) throw err;

         stream.on('ls', function(data, extended) {
              console.log(data);
              console.log(extended);
         });

    });

});

can someone direct me what im doing wrong or how it supposed to work ? btw there no connection problem.

thanks

Running commands via root on SSH2

I am developing a control panel in PHP, i have the PHPsecLib libraries installed and am using it to issue commands to the remote servers. For example; $ssh->exec(cd /home/{$user}/{$id}; screen -S



nodejs + ssh2 + nodessh

i have a question about ssh. var Connection = require(‘ssh2’); var c = new Connection(); c.on(‘connect’, function() { console.log(‘Connection :: connect’); }); c.on(‘ready’, function() { console.log(

PHP passing shell commands over SSH with special characters

I am passing dynamic shell commands over SSH to servers with PHP using the ssh2 library. The command I am passing is (note this is just an example, the commands are dynamic and user generated). $comma

Chaining SSH commands via PHP

I am running a SSH2 command via PHP and am trying to chain the commands to answer the password prompts from the bash script I run. wget url-to-bash-script bash name-of-file.sh After this it will prom

Unable to authenticate using nodejs with ssh2 module, even though manual ssh works

I am trying to use nodejs (v0.8.2) with the ssh2 module to grab some files from a remote machine (using sftp). I am able to ssh to the remote machine by hand using either a password or rsa keys, but s

Bundling shell commands in NodeJs spawn

I wish to run commands like NODE_ENV=production node server or cd ~/app/cms; npm test with NodeJs spawn First can be achieved with process.env.NODE_ENV = ‘production’ start = spawn ‘node’, [‘server’],

How to run commands via NodeJS child process?

I am trying to run commands on Windows via NodeJS child processes: var terminal = require(‘child_process’).spawn(‘cmd’); terminal.stdout.on(‘data’, function (data) { console.log(‘stdout: ‘ + data); })

SSH Shell Script multiple commands output

Grettings, I want to execute 2 commands on ssh within a shell script and retrieve the output to my local machine example ssh user@host command1 > /local/1.txt ssh user@host command2 > /local

Sending commands to server via Jsch shell channel

I can’t figure it out how I can send commands via Jsch shell channel. I do this, but it doesn’t work: JSch shell = new JSch(); String command = cd home/s/src; Session session = shell.getSession(use

What is advantage of using modules as ssh2 or control over exec command from local shell? [closed]

How to execute specific command (for example ls -al) on remote and fetch result success or failure in Node.js ? What is advantage of using modules as ssh2 or control over exec command from local shell

Answers

Here’s what you want:

c.on('ready', function() {
  c.exec('ls -lah', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data, stderr) {
      if (stderr)
        console.log('STDERR: ' + data);
      else
        console.log('STDOUT: ' + data);
    }).on('exit', function(code, signal) {
      console.log('Exited with code ' + code);
    });
  });
});

With the new version of ssh2 (0.3.6) in npm, its slightly changed now:

c.on('ready', function() {
  c.exec('ls -lah', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data) {
      console.log('STDOUT: ' + data);      
    }).stderr.on('data', function(data){
      console.log('STDERR: ' + data);      
    }).on('exit', function(code, signal) {
      console.log('Exited with code ' + code + ' and signal: ' + signal);
    });
  });
});