2.将生成的id_rsa.pub文件发送到被控端.也可以用别的方法,方法太多,不一一列举.
#!/bin/bash
#filename: root_pwd.sh
#Environment:
centos 5.6 32-bit
#Author: maoxian
#Blog: maoxian.blog.51cto.com
#定义变量
ip_list=`cat /root/change_pwd/ip_list.txt`
#定义一个自动生成随机密码的函数
function pwdgen {
strUp="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strLow="abcdefghijklmnopqrstuvwxyz"
strNum="0123456789"
passLen="8"
while [ "${#pass}" -le "$passLen" ]
do
passUp="${strUp:$(($RANDOM%${#strUp})):1}"
passLow="${strLow:$(($RANDOM%${#strLow})):1}"
passNum="${strNum:$(($RANDOM%${#strNum})):1}"
pass="$pass$passUp$passLow$passNum"
done
echo ${pass:0:$passLen}
}
#选择改密码的方式
read -p "密码随机生成输入a 密码手动设置输入b: " fangshi
if [ $fangshi = a ];then
TMP_PWD=`pwdgen`
elif [ $fangshi = b ]; then
read -p "请输入您要设置的密码: " TMP_PWD
else
echo " 输入错误 "
exit
fi
#密码修改执行
for IP in $ip_list; do
echo $TMP_PWD > TMP_PWD.txt
ssh $IP passwd root --stdin < TMP_PWD.txt
if [ $? = 0 ] ; then
echo -e "$(date "+%Y-%m-%d %H:%M:%S")t${IP}t${TMP_PWD}t" >> pwd_$(date +%Y-%m-%d).log
else
echo -e "$(date "+%Y-%m-%d %H:%M:%S")t${IP} Password change failstplease check!t" >> fails_$(date +%Y-%m-%d).log
fi
done
rm -f TMP_PWD.txt
echo "所有主机的密码修改已完成,请查看pwd_$(date +%Y-%m-%d).log文件!"