13

I'm trying to run a ssh child process in node.js and control it through my program. My code:

var util   = require('util');
var spawn = require('child_process').spawn;
var ssh    = spawn('ssh', ['cloudstudios.ch']);

ssh.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ssh.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ssh.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});

I can enter the password in the console, but I can't do anything after that. I get the following console ouput:

stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.

root@xxxxxxxxxxxxx.xx's password:
stdout: Linux v 2.6.32-5-xen-amd64 #1 SMP Wed Jan 12 05:46:49 UTC 2011 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

stderr: stdin: is not a tty

Has someone an idea how I can get this to work?

Thanks!

Van Coding
  • 24,244
  • 24
  • 88
  • 132

2 Answers2

10

try a little modification:

var ssh = spawn('ssh', ['-tt', 'xxx']);

and:

process.stdin.resume();
process.stdin.on('data', function (chunk) {
  ssh.stdin.write(chunk);
});
stack underflow
  • 159
  • 1
  • 3
  • sorry i dont actually know what those pseudo terminals are so no explanation, see man ssh. Also remember the command reset will come handy in these experiments. – stack underflow Mar 13 '11 at 22:28
  • Thanks! That TTY thing is now fixed! But after entering the password and hitting enter, I cannot see any output of ssh. Any idea for this? – Van Coding Mar 13 '11 at 22:28
  • I've tested if the commands I enter are executed. And they are! I only don't get the output of the commands. Have you an idea how to get the missing output? – Van Coding Mar 13 '11 at 22:33
  • Hey great! I made the mistake and replaced the part of listening to the process data event with part of listening to the stdin data event. Now it works perfectly! Thank you so much! – Van Coding Mar 13 '11 at 22:37
  • I'm trying to write the password programmatically to the ssh process. But I can't find a way? Do you know something I could do? – Van Coding Mar 15 '11 at 22:06
  • 2
    Use public key authentication instead? – andref Mar 16 '11 at 20:09
  • 1
    You can use pty.js for passoword sending http://stackoverflow.com/a/9788213/153718 – esamatti Mar 20 '12 at 13:58
8

I´ve created a SSHClient for node.js now. You can download the sourcecode at https://github.com/VanCoding/NodeSSH.

Hope it will help some other people.

esamatti
  • 18,293
  • 11
  • 75
  • 82
Van Coding
  • 24,244
  • 24
  • 88
  • 132