• shell通过ssh批量修改centos密码


    环境: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中
    echo `date +"%Y年%m月%d日-%k:%M"` >> 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
    

      

      

     

  • 相关阅读:
    【Redis】集群NetCore实战
    【Redis】集群教程(Windows)
    【Redis】入门
    【SQL SERVER】索引
    【SQL SERVER】锁机制
    【SQL SERVER】数据内部存储结构简单探索
    Windows软件包管理工具
    Git常用命令记录
    【ASP.NET Core学习】远程过程调用
    CouchDB学习-API
  • 原文地址:https://www.cnblogs.com/wukc/p/13938555.html
Copyright © 2020-2023  润新知