2011-03-13 56 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