我正在尝试使用期望将 $PASSWORD 变量内容传递给 passwd。这似乎可以添加用户,但是一旦您尝试使用其中一个用户通过 ssh 登录,它就不起作用了。如果我手动设置密码,那就没问题了。
以前有人遇到过这个问题吗?
USERS=(user1 user2 user3)
generatePassword ()
{
pwgen 16 -N 1
}
# Check if user is root
if [ $(whoami) != 'root' ]; then
echo "Must be root to run $0"
exit 1;
fi
# Check if pwgen is installed:
if [[ $(dpkg -s pwgen > /dev/null 2>&1; echo ${PIPESTATUS} ) != '0' ]]; then
echo -e "pwgen is not installed, this script will not work without it\n\n'apt-get install pwgen'\n"
exit 1;
else
echo -e "Starting Script...\n\n"
fi
# Iterate through users and add them with a password
for i in ${USERS[@]}; do
PASSWORD=$(generatePassword)
echo "$i $PASSWORD" >> passwords
useradd -m "${i}"
echo -e "Adding $i with a password of '$PASSWORD'\n"
expect -c "
spawn passwd ${i}
expect \"Enter new UNIX password:\"
send -- \"$PASSWORD\r\"
send -- \"\r\"
expect \"Retype new UNIX password:\"
send -- \"$PASSWORD\r\"
send -- \"\r\"
"
echo -e "\nADDED $i with a password of '$PASSWORD'\n"
done