Shell 实现 CentOS 7 下用户密码更改

脚本编写

#!/bin/bash

# get current date
currentDate=`date +%Y-%m-%d\ %H:%M:%S`
# set user for change pass
operationUser=root
# save new pass to file
fileDir="/home/superroot/allRootPass.txt"
# get old pass
oldPass=`tail -n 1 ${fileDir} | awk -F ' -- ' '{print $2}'`
# create new pass by openssl -- 16
newPass=`openssl rand -base64 16`


# main

echo "${newPass}" | passwd --stdin ${operationUser} && echo "[ ${currentDate} ] -- ${newPass}" >> ${fileDir}

计划任务配置

# crontab -e -u superroot
*/10 * * * * /usr/bin/sh /home/superroot/changeRootPass.sh

查看计划任务

# crontab -l -u superroot
*/10 * * * * /usr/bin/sh /home/superroot/changeRootPass.sh

查看执行结果

# cat /home/superroot/allRootPass.txt
[ 2020-07-28 14:09:35 ] -- m51DDOSutm1SQ3jeoUfFKA==
[ 2020-07-28 15:00:01 ] -- kR4uDuzbJQqtdIIw5HS+4g==

修改 allRootPass.txt 文件权限

# chmod 600 allRootPass.txt
# ll allRootPass.txt
-rw-------. 1 root root 296 7月  28 15:30 allRootPass.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值