首页 > 系统服务 > 详细

shell通过ssh批量修改centos密码

时间:2020-11-06 20:13:14      阅读:56      评论:0      收藏:0      [点我收藏+]

环境: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

  

  

 

shell通过ssh批量修改centos密码

原文:https://www.cnblogs.com/wukc/p/13938555.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!