一、利用centos自带发邮件工具实现
- 1,安装sendmail
- yum -y install sendmail
- systemctl start sendmail
- 2,安装mailx
- yum install -y mailx
- 3,配置文件
- vim /etc/mail.rc
结尾追加
set bsdcompat
set from=邮箱账号@163.com
set smtp=smtp.163.com
set smtp-auth-user=邮箱用户名
set smtp-auth-password=邮箱密码
set smtp-auth=login
二、更改密码脚本
vim password.sh
#!/bin/bash
ip=`ip a show dev ens33|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'` #这一行是提取本机IP的,不需要改
tr -dc _A-Z-a-z#$%^*-0-9 </dev/urandom |head -c20 >/home/password.txt #这一行是生成随机密码的
cat /home/password.txt |passwd root --stdin #这一行是将生成的密码本机的密码
SendStatus=`mail -v -s "$ip test-root-password" XXXX@163.com< /home/password.txt | grep -c "Mail Delivery Status Report will be mailed to <root>"` #这一行是将密码发送到指定邮箱,这里你们设置自己的邮箱
if [ "$SendStatus" == "1" ] ; then
echo "Sender mail ok"
fi
rm -rf /home/password.txt
exit
三、最后可以做个定时任务每月执行一次
crontab -e
0 0 1 * * /home/password.sh > /dev/null 2>&1
最后测试成功