Passing variable from Ruby as a password for shell

2019-08-20 03:05发布

I'd like to create a ruby script which acquires variable from user with "gets" and then passes this variable to a Linux shell password prompt.

An example would be:

sudo apt-get update

Which would produce:

[sudo] password for username:

And there Ruby script should pop in that variable so the update could be executed.

Any help will be appreciated!

标签: ruby shell
1条回答
神经病院院长
2楼-- · 2019-08-20 03:47

For security reasons, sudo doesn't accept passwords on standard input by default. You should configure your sudoers file with a NOPASSWD: tag for the commands you want to execute without prompting, and invoke sudo with the -n flag to ensure that your script doesn't get hung up waiting for input.

If you insist on passing in passwords, see if your sudo supports the -S flag, which (on my system) says:

   -S          The -S (stdin) option causes sudo to read the password from
               the standard input instead of the terminal device.  The
               password must be followed by a newline character.
查看更多
登录 后发表回答