问题描述
我正在编写一个 UNIX shell 函数,它将执行一个命令,提示用户输入密码.我想将密码硬编码到脚本中并将其提供给命令.我试过将密码输入到命令中,如下所示:
I'm writing a UNIX shell function that is going to execute a command that will prompt the user for a password. I want to hard-code the password into the script and provide it to the command. I've tried piping the password into the command like this:
function() {
echo "password" | command
}
这可能不适用于某些命令,因为该命令可能会在提示输入密码之前刷新输入缓冲区.
This may not work for some commands as the command may flush the input buffer before prompting for the password.
我也试过将标准输入重定向到一个包含像这样的密码的文件,但这也不起作用:
I've also tried redirecting standard input to a file containing the password like this, but that doesn't work either:
function() {
echo "password" > pass.tmp
command < pass.tmp
rm pass.tmp
}
我知道有些命令允许将密码作为参数提供,但我更愿意通过标准输入.
I know that some commands allow for the password to be provided as an argument, but I'd rather go through standard input.
我正在寻找一种快速而肮脏的方式将密码输入 bash 中的命令.
I'm looking for a quick and dirty way of piping a password into a command in bash.
推荐答案
看一看 autoexpect
(体面的教程 此处).这与您无需使用诡计即可获得的快速和肮脏一样.
Take a look at autoexpect
(decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.
这篇关于如何为在 bash 中提示输入的命令提供密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!