Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection.shell - data callback gets called once for every line of remote shell's output. #210

Closed
ghost opened this issue Jan 8, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented Jan 8, 2015

var Connection = require('ssh2');
var conn = new Connection();
conn.on('ready', function() {
  console.log('Connection :: ready');
  conn.shell(function(err, stream) {
    if (err) throw err;
    stream.on('close', function() {
      console.log('Stream :: close');
      conn.end();
    }).on('data', function(data) {
      console.log('STDOUT: ' + data);
    }).stderr.on('data', function(data) {
      console.log('STDERR: ' + data);
    });
    stream.end('ls -l\nexit\n');
  });
}).connect({
  host: '192.168.100.100',
  port: 22,
  username: 'frylock',
  privateKey: require('fs').readFileSync('/here/is/my/key')
});

// example output:
// Connection :: ready
// STDOUT: Last login: Sun Jun 15 09:37:21 2014 from 192.168.100.100
//
// STDOUT: ls -l
// exit
//
// STDOUT: frylock@athf:~$ ls -l
//
// STDOUT: total 8
//
// STDOUT: drwxr-xr-x 2 frylock frylock 4096 Nov 18  2012 mydir
//
// STDOUT: -rw-r--r-- 1 frylock frylock   25 Apr 11  2013 test.txt
//
// STDOUT: frylock@athf:~$ exit
//
// STDOUT: logout
//
// Stream :: close

This is the same sample reproduced from npm-ssh2 readme page. What i found to be intriguing is, the callback for stream.on('data') is called once for every line of output.
It will be easier to process data, if one shell command's output can be given in a single callback invocation.
And also, in on-data callback, the bytes that are written from the local computer are also given back to us (// STDOUT: ls -l // exit). This is mixing the input to the remote computer and the output.
I am not raising this as a bug but more keen on understanding what logic makes conn.shell behave like this.

@unknownbrackets
Copy link

Well, there are reasons for this. The shell is just printing characters to the screen, so it's repeating your typing back just like it would on a real terminal.

As far as the lines, my assumption is that the program is flushing every line of output, which is common / default behavior if I recall correctly. You can write your own program to execute and call setvbuf() or etc. to change this behavior.

What you have to keep in mind is that you're interacting with a shell, and it's communicating with you like you're a terminal.

-[Unknown]

@ghost
Copy link
Author

ghost commented Jan 8, 2015

that is a very practical explanation. thanks.

@TylerButh
Copy link

Is there any way to add a setting that will have it wait for a complete return before returning the data? That way everything is returned at once instead of split between multiple returns?

@mscdex
Copy link
Owner

mscdex commented Jan 9, 2015

@TylerButh I'm not sure what you mean. If you're talking about buffering all incoming data from the shell session, that's outside of the scope of this module. There may be other modules on npm that use this module that provide that kind of functionality however.

@ghost ghost closed this as completed Jan 9, 2015
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants