【问题标题】:Automatic writing text to the console using Node.js使用 Node.js 自动将文本写入控制台
【发布时间】:2014-09-05 22:37:29
【问题描述】:

我需要使用 SSH 和 Node.js 脚本克隆 GitHub 存储库:

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 会在“您确定要继续连接(是/否)吗?”的问题上强制输入“是”。

如何自动输入此文本?

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

【问题讨论】:

    标签: node.js ssh console automation


    【解决方案1】:

    当然,这完全有可能。当你调用child_process.exec 时,它实际上返回了一个ChildProcess 对象。它包含一个.stdin 对象,它是Writable Stream 的一个实现,您可以通过管道传输/写入该对象。 ChildProcess.stdinWritable Stream 上的文档。

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

    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");
    

    【讨论】:

      【解决方案2】:

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

      exec('yes | git clone git@github.com:jquery/jquery.git'...

      【讨论】:

      • 这不会将yes 放入STDIN,它会执行命令“yes”并通过管道输出。
      • 我想你的意思是exec('echo "yes" | git clone...')
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多