问题描述
我需要使用SSH和Node.js的脚本克隆GitHub的库:
I need to clone GitHub repository using SSH and Node.js script:
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迫使上的问题输入yes你确定要继续连接(是/否)?
If github.com not in known_hosts
file, SSH forcing to enter "yes" on the question "Are you sure you want to continue connecting (yes/no)?".
我如何可以自动完成这一文本的输入?
How can I automate the input of this text?
P.S。我知道 StrictHostKeyChecking =没有
,但我需要克隆库,而不改变SSH配置。
P.S. I know about StrictHostKeyChecking=no
, but I need to clone repository without changing SSH config.
推荐答案
当然,这是完全可能的。当你调用 child_process.exec
,它实际上返回子进程
对象。它包含一个 .stdin
对象,它是写流
的实现,你可以管/写。在ChildProcess.stdin,还对。
Sure, that is entirely possible. When you call child_process.exec
, it actually returns a ChildProcess
Object. It contains an .stdin
object which is an implementation of a Writable Stream
, which you can pipe to / write to. Documentation on ChildProcess.stdin, also on Writable Stream.
下面是一些例子code,涉及到你的问题:
Here is some example code that relates to your question:
var exec = require('child_process').exec;
var cmd = exec('git clone [email protected]:jquery/jquery.git', function (error, stdout, stderr) {
// ...
});
cmd.stdin.write("yes");
这篇关于自动书写文字使用Node.js的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!