如何在不回显的情况下从 shell 脚本获取密码
我有一个脚本可以自动执行需要访问受密码保护的系统的进程。该系统是通过命令行程序访问的,该程序接受用户密码作为参数。
我想提示用户输入密码,将其分配给 shell 变量,然后使用该变量构建访问程序的命令行(这当然会产生我将处理的流输出)。
我是 Bourne/Bash 中相当称职的 shell 程序员,但我不知道如何接受用户输入而不将其回显到终端(或者可能使用“*”字符进行回显)。
谁能帮忙解决这个问题吗?
I have a script that automates a process that needs access to a password-protected system. The system is accessed via a command-line program that accepts the user password as an argument.
I would like to prompt the user to type in their password, assign it to a shell variable, and then use that variable to construct the command line of the accessing program (which will of course produce stream output that I will process).
I am a reasonably competent shell programmer in Bourne/Bash, but I don't know how to accept the user input without having it echo to the terminal (or maybe having it echoed using '*' characters).
Can anyone help with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是另一种方法:
read -s
将为您关闭回显。只需将最后一行的echo
替换为您要运行的命令即可。在某些 shell 中(例如 Bash)
read
支持-p Prompt-string
允许组合echo
和read
命令:Here is another way to do it:
The
read -s
will turn off echo for you. Just replace theecho
on the last line with the command you want to run.In some shells (e.g. Bash)
read
supports-p prompt-string
which will allow theecho
andread
commands to be combined:符合 POSIX 标准的答案。请注意使用
/bin/sh
而不是/bin/bash
。 (它确实可以与 bash 一起使用,但它不需要需要 bash。)A POSIX compliant answer. Notice the use of
/bin/sh
instead of/bin/bash
. (It does work with bash, but it does not require bash.)一句话:
在 Linux(和 cygwin)下,这种形式可以在 bash 和 sh 中运行。但它可能不是标准的 Unix sh。
有关更多信息和选项,请在 bash 中键入“help read”。
One liner:
Under Linux (and cygwin) this form works in bash and sh. It may not be standard Unix sh, though.
For more info and options, in bash, type "help read".
POSIX 标准中没有定义
read
的-s
选项。请参阅http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read .html。我想要一些适用于任何 POSIX shell 的东西,所以我编写了一个使用 stty 来禁用 echo 的小函数。此函数的行为与
read
命令非常相似。以下是read
的简单用法,后面是read_secret
的类似用法。read_secret
的输入显示为空,因为它没有回显到终端。这是另一个使用
-r
选项来保留输入中的反斜杠的例子。这是有效的,因为上面定义的 read_secret 函数将其接收的所有参数传递给 read 命令。最后,这里是一个示例,展示如何使用
read_secret
函数以符合 POSIX 的方式读取密码。The
-s
option ofread
is not defined in the POSIX standard. See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html. I wanted something that would work for any POSIX shell, so I wrote a little function that usesstty
to disable echo.This function behaves quite similar to the
read
command. Here is a simple usage ofread
followed by similar usage ofread_secret
. The input toread_secret
appears empty because it was not echoed to the terminal.Here is another that uses the
-r
option to preserve the backslashes in the input. This works because theread_secret
function defined above passes all arguments it receives to theread
command.Finally, here is an example that shows how to use the
read_secret
function to read a password in a POSIX compliant manner.我发现
askpass
命令很有用每个输入字符都会被 * 替换。看:
给个密码****
I found to be the the
askpass
command usefulEvery input character is replaced by *. See:
Give a password ****
虽然已经有很多答案,但在几乎任何运行
systemd
的现代linux
中,还有另一种方法可以从终端询问密码(是的,嘘,我知道,但是你能做些什么呢)。 systemd-ask-password 命令包含在标准核心中systemd
包,可以像这样使用:使用
--emoji=no
开关来抑制那个愚蠢的 unicode 锁定字符。它在终端内工作得很好,但如果您需要它弹出 GUI 对话框,则需要进行调整,但这超出了此处的范围。While there are plenty of answers already, there is one more way to ask for passwords from terminal in virtually any modern
linux
that runssystemd
(yes, booo, I know, but what can you do about it). systemd-ask-password command is included with standard coresystemd
package and can be used like this:Use
--emoji=no
switch to suppress that stupid unicode lock character. It works great inside terminal but requires tweaking if you need it to pop up a GUI dialog, but that is out of scope here.您还可以通过执行以下操作来提示输入密码,而无需在当前 shell 中设置变量:
例如:
您可以使用换行符添加其中几个提示值,执行以下操作:
You can also prompt for a password without setting a variable in the current shell by doing something like this:
For instance:
You can add several of these prompted values with line break, doing this:
使用
stty
关闭echo
,然后再次打开。Turn
echo
off usingstty
, then back on again after.对于需要提示输入密码的人,您可能有兴趣使用 encpass.sh。这是我出于类似目的而编写的脚本,即在运行时捕获秘密,然后对其进行加密以供后续场合使用。后续运行不会提示输入密码,因为它将仅使用磁盘中的加密值。
它将加密的密码存储在用户主目录下的隐藏文件夹中,或者存储在您可以通过环境变量 ENCPASS_HOME_DIR 定义的自定义文件夹中。它被设计为符合 POSIX 标准并拥有 MIT 许可证,因此它甚至可以在公司企业环境中使用。我的公司 Plyint LLC 维护该脚本并偶尔发布更新。如果您发现问题,也欢迎拉取请求。 :)
要在脚本中使用它,只需在脚本中获取 encpass.sh 并调用 get_secret 函数。为了便于查看,我附上了下面脚本的副本。
For anyone needing to prompt for a password, you may be interested in using encpass.sh. This is a script I wrote for similar purposes of capturing a secret at runtime and then encrypting it for subsequent occasions. Subsequent runs do not prompt for the password as it will just use the encrypted value from disk.
It stores the encrypted passwords in a hidden folder under the user's home directory or in a custom folder that you can define through the environment variable ENCPASS_HOME_DIR. It is designed to be POSIX compliant and has an MIT License, so it can be used even in corporate enterprise environments. My company, Plyint LLC, maintains the script and occasionally releases updates. Pull requests are also welcome, if you find an issue. :)
To use it in your scripts simply source encpass.sh in your script and call the get_secret function. I'm including a copy of the script below for easy visibility.