【问题标题】:Run shell command and reply to questions运行shell命令并回答问题
【发布时间】:2014-02-10 13:26:36
【问题描述】:

我有一个 shell 脚本,它在启动时会要求提供凭据。

我想使用 exec() 使用 Node.js 启动我的脚本,并在询问时发送凭据。

目前我的代码是这样的:

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("myscript", puts);

有可能吗?

n.b.我无法将凭据作为脚本的参数传递,它不支持它们。

【问题讨论】:

  • 什么样的凭据?操作系统的凭据或某些服务的凭据?是因为您无权以当前用户身份运行脚本并且需要提升权限吗?您使用的是哪种操作系统?
  • 是特定于脚本的凭据(实际上是“git”)。它应该可以在 Window/OSx/Linux 上运行(但没关系,因为 git 有自己的 shell)。
  • 解决这个问题的一个想法是在您的 git 帐户中使用 ssh 密钥。这样您就根本不需要凭据。
  • 很遗憾我不能以这种方式继续,这就是为什么我没有在我的问题中谈到 GIT,以防止这种回复。

标签: node.js shell


【解决方案1】:

您应该写入进程的标准输入。这是exec 的示例:

var exec = require('child_process').spawn;
var child = spawn('script');

child.stdin.setEncoding('utf8');

child.stdout.on('data', function (data) {
  if (data.toString() === 'enter the password:'){
    child.stdin.write('pass');
    return child.stdin.end();
  }
  console.log('got some other data:');
  console.log(data.toString())
});

Similar to this question.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多