javascript 在 Node.js 上使用 SSH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5292757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:40:20  来源:igfitidea点击:

Using SSH over Node.js

javascriptsshnode.jschild-process

提问by Van Coding

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

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

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.

[email protected]'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!

谢谢!

采纳答案by stack underflow

try a little modification:

尝试一点修改:

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

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

and:

和:

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

回答by Van Coding

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

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

Hope it will help some other people.

希望它会帮助其他一些人。