检查一个shellcript中的unix用户名和密码

5 unix shell

如果本地unix-user的用户名和密码正确,我想检查一个shell脚本.最简单的方法是什么?

我在谷歌搜索时发现的只是使用'expect'和'su',然后以某种方式检查'su'是否成功.

Hay*_*ayt 8

用户名和密码写在/etc/shadow文件中.从那里获取用户和密码哈希(sed会有所帮助),哈希自己的密码并检查.

使用mkpasswd生成哈希.你必须看看你的版本使用的是哪种盐.最新的阴影正在使用sha-512:

mkpasswd -m sha-512 password salt
Run Code Online (Sandbox Code Playgroud)

联机帮助页可以为您提供很多帮助.

更容易使用php和pam-aut模块.在那里你可以检查组访问pwd用户的PHP.


小智 5

好的,现在这是我用来解决问题的脚本。我首先尝试按照 Aaron Digulla 的建议编写一个小型 C 程序,但事实证明这太困难了。

也许这个脚本对其他人有用。

#!/bin/bash
#
# login.sh $USERNAME $PASSWORD

#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
        echo "This script can't be run as root." 1>&2
        exit 1
fi

if [ ! $# -eq 2 ]; then
        echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
        exit 1
fi

USERNAME=$1
PASSWORD=$2

# Setting the language to English for the expected "Password:" string, see http://askubuntu.com/a/264709/18014
export LC_ALL=C

#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit" 
expect "Password:"
send "$PASSWORD\r"
#expect eof

set wait_result  [wait]

# check if it is an OS error or a return code from our command
#   index 2 should be -1 for OS erro, 0 for command return code
if {[lindex \$wait_result 2] == 0} {
        exit [lindex \$wait_result 3]
} 
else {
        exit 1 
}
EOF
Run Code Online (Sandbox Code Playgroud)