有个需求:很多领导都需要访问服务器,使用htpasswd加密,不想一个一个去设置
写了如下脚本:分为3部分,gaby.txt(用于存账户,密码的文件)两个执行脚本(gaby.exp,gaby.sh)和一个生成文件gaby.pl。
vim gaby.txt
1 abcde
2 abcde
c abcde
vim gaby.exp
#!/usr/bin/expect -f    
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
vim gaby.sh
#!/bin/bash    
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
#bash -x gaby.sh