我正在尝试使用expect将$ 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
答案 0 :(得分:0)
首先:最直接的问题是,当您输入\r
时,您没有转义反斜杠文字,因此这些只会更改为r
侧的expect
s ;似乎解决问题的最小可能变化是将其更改为\\r
。
但是 - 不要那样做:在expect
和其他语言一样,字符串应该作为文字传递,而不是代入代码。
expect -f <(printf '%s\n' '
set username [lindex $argv 0];
set password [lindex $argv 1];
spawn passwd $username
expect "Enter new UNIX password:"
send -- "$password\r"
send -- "\r"
expect "Retype new UNIX password:"
send -- "$password\r"
send -- "\r"
') -- "$i" "$PASSWORD"
您还可以将相关的文字文本保存到文件中,然后运行expect -f passwd.expect -- "$i" "$PASSWORD"
,这也可以正常工作(并避免依赖于<()
语法,这是bash采用的ksh扩展名)。
答案 1 :(得分:0)
您根本不需要:使用chpasswd
代替passwd
#!/bin/bash
users=(user1 user2 user3)
# Check if user is root
if [[ "$(id -un)" != '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; then
printf "pwgen is not installed, this script will not work without it\n\n'apt-get install pwgen'\n"
exit 1
else
printf "Starting Script...\n\n"
fi
# Iterate through users and create a password
passwords=()
for user in "${users[@]}"; do
useradd -m "$user"
password="$user:$(pwgen 16 -N 1)"
passwords+=("$password")
echo "Adding user '$user' with '$password'"
done
printf "%s\n" "${passwords[@]}" | chpasswd
我添加了几个必需的引号 我已经简化了dpkg检查。
或者,或许更简单,newusers
来执行&#34; useradd&#34; &#34; passwd&#34;原子地运作。
for user in "${users[@]}"; do
password="$user:$(pwgen 16 -N 1)"
password=${password//:/-} # replace all colon with hyphen
printf "%s:%s::::/home/%s:/bin/bash\n" "$user" "${password//:/-}" "$user"
done | newusers
我不认为新用户会从/ etc / skel填充主目录。