在Node.js上使用SSH

Van*_*ing 13 javascript ssh child-process node.js

我正在尝试在node.js中运行一个ssh子进程,并通过我的程序控制它.我的代码:

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);
});
Run Code Online (Sandbox Code Playgroud)

我可以在控制台输入密码,但之后我什么也做不了.我得到以下控制台输出:

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
Run Code Online (Sandbox Code Playgroud)

有人知道我怎么能让这个工作?

谢谢!

小智 11

尝试一点修改:

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

和:

process.stdin.resume();
process.stdin.on('data', function (chunk) {
  ssh.stdin.write(chunk);
});
Run Code Online (Sandbox Code Playgroud)

  • 使用公钥身份验证代替? (2认同)

Van*_*ing 7

我现在为node.js创建了一个SSHClient.您可以在https://github.com/VanCoding/NodeSSH下载源代码.

希望它能帮助其他人.