自动书写文字使用Node.js的控制台 [英] Automatic writing text to the console using Node.js

查看:157
本文介绍了自动书写文字使用Node.js的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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 git@github.com: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 对象,它是写流的实现,你可以管/写。在<一个文档href=\"http://nodejs.org/docs/latest/api/child_process.html#child_process_child_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 git@github.com:jquery/jquery.git', function (error, stdout, stderr) {
    // ...
});

cmd.stdin.write("yes");

这篇关于自动书写文字使用Node.js的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆