#!/bin/bash

for ip in `cat iplist.txt`

do

pwd=`mkpasswd -l 12 -s 0 -C 0`

grep -q $ip /root/pwd.log&&passwd=$(grep $ip /root/pwd.log|cut -d: -f2)||passwd=123456

expect -c "

        spawn ssh root@$ip  passwd

        expect {

                        \"*yes/no*\" { send \"yes\r\";exp_continue }

                        \"*password*\" { send \"$passwd\r\"; }

                        }

        expect { 

                \"*New password:\"  { send \"$pwd\r\";exp_continue } 

                \"Retype new password:\" { send \"$pwd\r\";exp_continue } 

                \"*#\" { send \"exit\r\"; }

        }

"

[[ -f /root/pwd.log ]]&&sed -i "/^$ip/d" /root/pwd.log

echo "$ip:$pwd" >>/root/pwd.log

done

其中passwd=123456是初始密码

新密码放在/root/pwd.log里面

ip列表放在脚本目录下的iplist.txt里面