环境:centos7
思路:ssh与要修改密码免密。脚本读取文本中ip地址,然后远程修改密码,并将密码放入文本中。
1、准备ip地址文本
[root@186 ~]# cat ip 100.98.100.186 100.98.100.188
2、编写批量修改密码脚本
[root@186 ~]# more change-passwd.sh #!/bin/bash source /etc/profile #人机交互是否执行批量修改密码 while true;do stty -icanon min 0 time 100 echo -n "Automatic execute ten seconds after,Are you sure you want to start the task(yes or no)?" read Arg case $Arg in Y|y|YES|yes) break;; N|n|NO|no) exit;; "") #Autocontinue break;; esac done #读取ip地址使用随机数作为密码修改root密码输入到文件ma.txt中 for ip in `cat ip` do p=`< /dev/urandom tr -dc 0-9-A-Z-a-z-/|head -c ${1:-10};echo` echo $p echo $ip ssh $ip "echo $p|passwd --stdin root" if [ $? = ‘0‘ ] then echo $ip密码$p >> ma.txt else echo ‘faild‘ fi done
3、赋权并执行
[root@186 ~]# chmod u+x change-passwd.sh [root@186 ~]# ./change-passwd.sh Automatic execute ten seconds after,Are you sure you want to start the task(yes or no)?yes zYUkJJNxEr 100.98.100.186 Changing password for user root. passwd: all authentication tokens updated successfully. 42trLHphkA 100.98.100.188 Changing password for user root. passwd: all authentication tokens updated successfully. [root@186 ~]# cat ma.txt 100.98.100.186密码zYUkJJNxEr 100.98.100.188密码42trLHphkA