利用expect交互批量修改普通用户过期密码

利用expect交互批量修改普通用户过期密码

因为公司接了一个密码托管的活,有10000多台机器,系统有 openSUSE,SUSE,Ubuntu,centos,Oracle-Linux,

而且什么版本都有,不给root权限,密码已经过期很多,先用expect的方式把过期密码修改完成,在进行托管

#!/bin/bash
#Author:shsnc-****
#Time:2021-5-11
#Name:for_user.sh
#Version:V1.3
#Description:This is a password check  script.


>passwd.info
>passwd.error

old_passwd=$1
new_passwd=$2
user=gziaas

# 定义并发
thread=10
fifofile="/tmp/$$.fifo"
mkfifo $fifofile 
exec 8<>${fifofile}
rm -f ${fifofile}

for pid in `seq ${thread}` 
do 
    echo >&8

done


# 定义修改函数
function change_passwd_ssh(){
 /usr/bin/expect << EOF
       set timeout 20
       spawn /usr/bin/ssh ${user}@$ip
       expect {
           "(yes/no)?" {send "yes\r";exp_continue}
           "*assword:" {send "${old_passwd}\r";}
       }

       expect {
            "(current) UNIX password:" {send "${old_passwd}\r";}
            "Old Password:" {send "${old_passwd}\r";}
       }

       expect {
              "New *assword:" {send "${new_passwd}\r";}
              "Retype new password:" {send "$new_passwd\r";} 
       }

       expect {
              "Retype new password:" {send "$new_passwd\r";}
       }

       expect {
              "Reenter New Password:" {send "$new_passwd\r";}
              "Retype new password:" {send "$new_passwd\r";}
       }


      send "exit\r"
      expect eof

EOF
}

# 脚本主体

for ip in `cat ip_list.txt`
do
    read -u 8
    {
       change_passwd_ssh $1 $2    1>>./passwd.info 2>>./passwd.error
       if [ $? -eq 0 ];then
           echo "${ip}" >>./successful.ip
       else 
           echo "${ip}" >>./fail.ip
       fi
       echo >&8
    }&   
done
wait
exec 8>&-
posted @ 2020-11-09 15:38  海上月  阅读(463)  评论(0编辑  收藏  举报