shell通过ssh批量修改centos密码

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中
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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值