javascript - 在 Node.js 上使用 SSH

标签 javascript ssh node.js child-process

我正在尝试在 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);
});

我可以在控制台中输入密码,但之后我无法执行任何操作。我得到以下控制台输出:

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

有人知道我怎样才能让它工作吗?

谢谢!

最佳答案

尝试一些修改:

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

和:

process.stdin.resume();
process.stdin.on('data', function (chunk) {
  ssh.stdin.write(chunk);
});

关于javascript - 在 Node.js 上使用 SSH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5292757/

相关文章:

javascript - 序列化 JS 对象以供离线使用

node.js - 尝试在 Express JS 中使用 Mustache 时出现 TypeError : this. 引擎不是函数

javascript - 在 node.js 中,如何判断发出的事件的来源?

javascript - react ,正确更新复选框

javascript - Material-UI 在生产模式下有不同的样式结果?

javascript - 在 JQuery 中检索字符串开头的整数

ssh - SSHv2 DH key 交换中的大安全素数

macos - 无法打开显示错误

linux - 通过 ssh 在远程机器上运行 7z

javascript - 在 Electron 中捕获、修改然后输出音频