使用 Node.js 自动将文本写入控制台 [英] Automatic writing text to the console using Node.js

查看:40
本文介绍了使用 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 强制在问题您确定要继续连接(是/否)吗?"时输入是".

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)?".

如何自动输入此文本?

附言我知道 StrictHostKeyChecking=no,但我需要在不更改 SSH 配置的情况下克隆存储库.

P.S. I know about StrictHostKeyChecking=no, but I need to clone repository without changing SSH config.

推荐答案

当然,这是完全可能的.当您调用 child_process.exec 时,它实际上返回一个 ChildProcess 对象.它包含一个 .stdin 对象,它是一个 Writable Stream 的实现,您可以通过管道传输到/写入.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.

以下是一些与您的问题相关的示例代码:

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天全站免登陆