1.编写执行脚本
vim host-pass.sh
#!/bin/bash
#配置旧的密码文件
cat >old_pass.txt <<EOF
10.36.192.182 root 123 22
10.36.192.184 root 123 22
EOF
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
#user--> root
USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)
#pass-->password
PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)
#port-->22
PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)
#新密码-->8位随机数
#NEW_PASS=1
NEW_PASS=$(openssl rand -base64 8)
# sshpass是一个工具,用于自动输入密码来建立SSH连接;StrictHostKeyChecking=no 选项告诉SSH客户端跳过主机密钥的检查,
#下载sshpass
yum -y install sshpass &>>/dev/null
sshpass -p "$PASS" ssh -p $PORT $USER@$IP -o StrictHostKeyChecking=no "echo $NEW_PASS |passwd --stdin $USER" && \
(
echo "$IP $USER $NEW_PASS $PORT [`date '+%F %T'`]" >> $NEW_INFO && \
action "配置成功 $USER $IP $NEW_PASS" /bin/true
) || \
action "配置失败 $USER $IP $PASS" /bin/false
done
2.测试时设置密码为1
3.
3.取随机密码测试
NEW_PASS=$(openssl rand -base64 8)