centos7下密碼定期自動更改並用mail發郵件

一、利用centos自帶發郵件工具實現

  1. 1,安裝sendmail
  2. yum -y install sendmail
  3. systemctl start sendmail
  4.  
  5. 2,安裝mailx
  6. yum install -y mailx
  7.  
  8. 3,配置文件
  9. 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" [email protected]< /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

 

最後測試成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章