我正在尝试在Apache的.htpasswd中自动设置用户密码,但是这个脚本似乎并没有真正设置密码。我错过了什么?谢谢!
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
cat << EOF | /usr/bin/expect
spawn /usr/bin/htpasswd -c /var/.htpasswd testuser
expect "assword:"
send "$PASSWORD\r"
expect "assword:"
send "$PASSWORD\r"
EOF
echo -e "\nPassword set to: $PASSWORD\n"
答案 0 :(得分:5)
您可以使用-b
标志在命令行上指定密码。
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
htpasswd -cb /var/.htpasswd testuser $PASSWORD
echo Password set to: $PASSWORD
# output:
# Adding password for user testuser
# Password set to: gzu00n4lp8
为了澄清,这是使用expect
的版本:
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
expect << EOF
spawn htpasswd -c /var/.htpasswd testuser
expect {
"New password:" { send "$PASSWORD\r"; exp_continue }
"Re-type new password:" { send "$PASSWORD\r"; exp_continue }
}
EOF
echo Password set to: $PASSWORD