2014-09-05 17 views
2

我需要使用SSH和Node.js的腳本克隆的GitHub庫控制檯:自動書寫文本使用Node.js的

var exec = require('child_process').exec; 

exec('git clone [email protected]:jquery/jquery.git', 
    function (error, stdout, stderr) { 
     console.log('stdout: ' + stdout); 
     console.log('stderr: ' + stderr); 
     if (error !== null) { 
      console.log('exec error: ' + error); 
     } 
    } 
); 

如果github.com不是known_hosts文件,SSH迫使進入「是」在「您確定要繼續連接(是/否)嗎?」這個問題上。

如何自動輸入本文?

P.S.我知道StrictHostKeyChecking=no,但我需要在不更改SSH配置的情況下克隆存儲庫。

回答

5

當然,這是完全可能的。當您致電child_process.exec時,它實際上會返回一個ChildProcess對象。它包含一個.stdin對象,該對象是Writable Stream的一個實現,您可以將其連接到/寫入到該對象。有關ChildProcess.stdin的文檔,還有Writable Stream

下面是一些示例代碼,涉及到你的問題:

var exec = require('child_process').exec; 

var cmd = exec('git clone [email protected]:jquery/jquery.git', function (error, stdout, stderr) { 
    // ... 
}); 

cmd.stdin.write("yes"); 
-1

你可以pass in yes之前你的git clone命令。

exec('yes | git clone [email protected]:jquery/jquery.git'...

+0

這不把'yes'到STDIN,它執行命令「是」和管道該輸出。 – 2014-09-06 17:01:30