问题描述
我正在编写一个 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 中提示输入密码的命令提供密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!