一个shell批量更改密码的脚本

发布时间:2019-10-02编辑:脚本学堂
一个shell批量更改密码的脚本

一、生成ssh秘钥对:
 

复制代码 代码如下:
#!/bin/bash
for ip in iplist.txt
do
ssh-copy-id -i  ~/.ssh/id_rsa.pub  $ip:/home
done

二、运行下面的脚本
 

复制代码 代码如下:

#!/bin/bash
#filename: root_pwd.sh
#Environment: centos

 #....
ip_list=`cat /home/shell/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}
}
#-------------------------------------
#更改root密码
read -p "root用户输入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
echo "$(date +%Y-%m-%d).log..!"
#修改普通用户密码
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 > pu.txt
ssh  $IP passwd baocheng --stdin < pu.txt
echo "$(date +%Y-%m-%d).log..!"
  done
done