利用expect交互批量修改普通用户密码
#!/bin/bash
#Author:shsnc-chenhaijun
#Time:2020-10-23
#Name:for_user.sh
#Version:V1.0
#Description:This is a password change script.
user=$1
old_passwd=$2
new_passwd=$3
d_time=`date +"%F-%H-%M"`
#ios_name=`lsb_release -a| awk -F '[: ]+' 'NR==2{print $3}' |sed 's/^[ ]*//g'`
#iod_name="bash ios.sh"
#Define modification function
function change_passwd(){
for ip in `cat ip_list.txt`
do
if [ $# -ne 3 ]
then
echo "Please check whether the transmission is correct"
echo "$0 Usage:username|the old passwd|The new password"
exit 0
fi
timeout 3 sshpass -p ${old_passwd} ssh -o StrictHostKeyChecking=no ${user}@${ip} hostname >>./change_passwd_${d_time}.log 2>&1
if [ $? -ne 0 ]
then
echo "username or passwd error Please check"
echo "${ip}:${user}:${old_passwd}" >>./old_passwd_${d_time}.txt
continue
elif [ '${ios_name}'=='CentOS' ] || [ '${ios_name}'=='RedHat' ] || [ '${ios_name}'=='openSUSE' ]
then
/usr/bin/expect << EOF
set timeout 1
spawn /usr/bin/ssh ${user}@$ip
expect {
"(yes/no)?" {send "yes
";exp_continue}
"*assword" {send "${old_passwd}
"}
}
expect "*]#"
send "passwd
"
expect {
"*assword:" { send "${old_passwd}
" }
}
expect {
"New password:" { send "${new_passwd}
" }
}
expect {
"Retype new password:" { send "${new_passwd}
" }
}
expect "*]#"
send "exit
"
expect eof
EOF
echo "${ip}:${user}:${new_passwd}">>./new_passwd_${d_time}.txt
else
echo "${ip}:${user}:${old_passwd}">>./old_passwd_${d_time}.txt
fi
done
}
change_passwd $1 $2 $3