提示:请提前定义ip.txt中的地址池 成功的会输出在newpass.txt中 失败的会输出在failip.txxt中

#!/bin/bash
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then 
    yum -y install expect &>/dev/null
fi
 
#判断公钥是否存在
if [ ! -f ~/.ssh/id_rsa ];then
    ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

echo "请先定义好IP地址池在当前目录的ip.txt"
sleep 2
#推公钥
for ip1 in `cat /root/ip.txt`
do
password="656768" #定义对方的密码 
/usr/bin/expect -c "   
	set timeout 5
        spawn ssh-copy-id root@$ip1
        expect {
         \"*(yes/no)\" { send \"yes\r\";exp_continue }
        \"*password\" { send \"$password\r\"; exp_continue }
        }     
expect eof"
done


#修改密码
> newpass.txt
> failip.txt
for ip in `cat /root/ip.txt`
do
math=`date +%N`
math2=`echo ${math:1:8}`
remote_cmd="echo $math2 | passwd --stdin root"
	ssh root@$ip $remote_cmd
	 if [ $? != 0 ];then
        echo "$ip连接失败 请手动检查!" >> failip.txt
        else
 	echo " 密码:($math2) ip地址:$ip  " >> newpass.txt
	fi 
done
cat newpass.txt
cat failip.txt

--------------------当你发现自己突然嫉妒起其他人时,就请安静下来学习吧!--------------------