我有一个脚本可以自动执行需要访问受密码保护的系统的进程。 通过命令行程序访问系统,该程序接受用户密码作为参数。
我想提示用户键入他们的密码,将其分配给shell变量,然后使用该变量构建访问程序的命令行(这当然会产生我将处理的流输出)。
我是Bourne / Bash中一个相当称职的shell程序员,但我不知道如何接受用户输入而不让它回显到终端(或者可能使用'*'字符回显)。
有人能帮忙吗?
#1楼
一个班轮:
read -s -p "Password: " password
在Linux(和cygwin)下,这个表单适用于bash和sh。 但它可能不是标准的Unix sh。
有关更多信息和选项,请在bash中键入“help read”。
$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.
...
-p prompt output the string PROMPT without a trailing newline before
attempting to read
...
-s do not echo input coming from a terminal
#2楼
该-s的选项read未在POSIX标准定义。 请参见http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html 。 我想要一些适用于任何POSIX shell的东西,所以我编写了一个使用stty来禁用echo的小函数。
#!/bin/sh
# Read secret string
read_secret()
{
# Disable echo.
stty -echo
# Set up trap to ensure echo is enabled before exiting if the script
# is terminated while echo is disabled.
trap 'stty echo' EXIT
# Read secret.
read "$@"
# Enable echo.
stty echo
trap - EXIT
# Print a newline because the newline entered by the user after
# entering the passcode is not echoed. This ensures that the
# next line of output begins at a new line.
echo
}
此函数的行为与read命令非常相似。 以下是read的简单用法,后面是read_secret的类似用法。 read_secret的输入显示为空,因为它未回显到终端。
[susam@cube ~]$ read a b c
foo \bar baz \qux
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=bar c=baz qux
[susam@cube ~]$ unset a b c
[susam@cube ~]$ read_secret a b c
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=bar c=baz qux
[susam@cube ~]$ unset a b c
这是另一个使用-r选项来保留输入中的反斜杠。 这是有效的,因为上面定义的read_secret函数将它接收的所有参数传递给read命令。
[susam@cube ~]$ read -r a b c
foo \bar baz \qux
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=\bar c=baz \qux
[susam@cube ~]$ unset a b c
[susam@cube ~]$ read_secret -r a b c
[susam@cube ~]$ echo a=$a b=$b c=$c
a=foo b=\bar c=baz \qux
[susam@cube ~]$ unset a b c
最后,这是一个示例,说明如何使用read_secret函数以符合POSIX的方式读取密码。
printf "Password: "
read_secret password
# Do something with $password here ...
#3楼
首先,如果有人要在文件中存储任何密码,我会确保它已经散列。 这不是最好的安全性,但至少它不会是纯文本。
首先,创建密码并将其哈希:echo "password123" | md5sum | cut -d '-' -f 1 > /tmp/secret
现在,创建您的程序以使用哈希。 在这种情况下,这个小程序接收用户输入密码而不回显,然后将其转换为散列以与存储的散列进行比较。 如果它与存储的哈希匹配,则授予访问权限:#!/bin/bash PASSWORD_FILE="/tmp/secret" MD5_HASH=$(cat /tmp/secret) PASSWORD_WRONG=1 while [ $PASSWORD_WRONG -eq 1 ] do echo "Enter your password:" read -s ENTERED_PASSWORD if [ "$MD5_HASH" != "$(echo $ENTERED_PASSWORD | md5sum | cut -d '-' -f 1)" ]; then echo "Access Deniend: Incorrenct password!. Try again" else echo "Access Granted" PASSWORD_WRONG=0 fi done
#4楼
使用stty关闭echo ,然后再重新打开。
#5楼
符合POSIX的答案。 注意使用/bin/sh而不是/bin/bash 。 (它确实与bash一起使用,但它不需要 bash。)
#!/bin/sh
stty -echo
printf "Password: "
read PASSWORD
stty echo
printf "\n"