我怎么能让grunt运行一个提示输入sudo密码的远程命令呢



我正在使用grunt,我需要它在需要sudo访问的远程机器上执行脚本,因此会提示输入密码。

我希望它提示输入密码,因为我不想将密码提交给源代码管理。

从命令行看,以下各项工作正常:

ssh -t examplehost.com "sudo ls"

然而,当我通过grunt exec运行相同的命令时,我会得到以下内容:

$ grunt exec:remote_sudo
Running "exec:remote_sudo" (exec) task
>> Pseudo-terminal will not be allocated because stdin is not a terminal.
>> sudo: no tty present and no askpass program specified
>> Sorry, try again.
>> sudo: no tty present and no askpass program specified
>> Sorry, try again.
>> sudo: no tty present and no askpass program specified
>> Sorry, try again.
>> sudo: 3 incorrect password attempts
>> Exited with code: 1.
Warning: Task "exec:remote_sudo" failed. Use --force to continue.
Aborted due to warnings.

对于这个有类似错误的问题的答案建议添加一个额外的-t开关来强制伪终端tty分配。

这样做会显示密码提示,但有些地方不对,因为当我键入密码时,它会清楚地显示我的密码(通常的行为在您键入时不会显示输出),并且它永远挂起:

$ grunt exec:remote_sudo
Running "exec:remote_sudo" (exec) task
[sudo] password for tom: not_my_password
# hangs forever here

我需要更改什么才能运行此脚本?我对grunt-exec的替代品持开放态度,但如果可能的话,我想坚持使用grunt。


示例Gruntfile.js供参考:

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  grunt.initConfig({
    exec: {
      remote_sudo: 'ssh -t examplehost.com "sudo ls"',
    }
  });
};

一个可能的解决方案是通过管道将密码发送到命令,如这个咕哝的ssh票证中所示。

echo password | sudo -S ls

您可以将密码作为参数传递给任务;类似CCD_ 5。这是一个完全未经测试的Gruntfile,可能会起作用:

module.exports = function(grunt) {
  grunt.initConfig({
    exec: {
      remote_sudo: {
        cmd: function(password) {
          return 'echo ' + password + ' | ssh -t examplehost.com "sudo ls"';
        }
      }
    }
  });
  grunt.loadNpmTasks('grunt-exec');
  grunt.registerTask('ssh_task', 'run remote command', function(password) {
    grunt.task.run(['exec:remote_sudo:' + password]);
  });
};

这里的缺点是,当你输入密码时,它仍然可见,并且会出现在你的shell历史记录中。

最新更新