写了如下脚本:分为3部分,gaby.txt(用于存账户,密码的文件)两个执行脚本(gaby.exp,gaby.sh)和一个生成文件gaby.pl。
2 abcde
c abcde
set name [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn htpasswd -d /root/gaby/gaby.pl $name
set timeout 300
expect "password:"
send "$passwd\r"
set timeout 300
expect "password:"
send "$passwd\r"
expect eof
if [ -e /root/gaby/gaby.pl ];then
echo "the file is exiting"
else
touch /root/gaby/gaby.pl
fi
cat gaby.txt | while read line
do
name=$(echo $line | awk '{print $1}')
passwd=$(echo $line|awk '{print $2}')
/usr/bin/expect /root/gaby/gaby.exp $name $passwd
[ $? -eq 0 ] && echo -e "\033[32mmake $name password is ok......\033[0m" || exit 10
sleep 2
done