2011-03-13 28 views
10

我试图在node.js中运行ssh子进程并通过我的程序进行控制。我的代码:在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. 

[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 

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

谢谢!

+0

看看https://github.com/steelbrain/node-ssh – 2015-05-23 06:26:08

回答

11

试了一下修改:

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

和:

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

对不起,我真的不知道那些伪终端是如此没有解释,请参阅man ssh。另外请记住命令重置将在这些实验中派上用场。 – 2011-03-13 22:28:39

+0

谢谢! TTY的东西现在已经修复了!但输入密码并按回车后,我看不到任何ssh的输出。任何想法呢? – 2011-03-13 22:28:52

+0

我测试过我输入的命令是否执行。他们是!我只是没有得到命令的输出。你有一个想法如何获得缺失的输出? – 2011-03-13 22:33:40

6

从来就创造了一个的Node.js现在SSHClient。您可以在https://github.com/VanCoding/NodeSSH下载源代码。

希望它能帮助别人。

+0

中断链接,新位置是https://github.com/VanCoding/NodeSSH – mindeavor 2012-02-15 20:34:36

+0

是的,我改了我的名字,对不起。 – 2012-02-16 08:32:39

+1

这是否适用于Windows上的PuTTY? – 2014-02-25 21:30:44