#!/bin/bash

#添加计划任务
#mkdir -p /scripts/
#touch /scripts/Passwd_expire_check.sh
#vim /scripts/Passwd_expire_check.sh
#chmod 755 /scripts/Passwd_expire_check.sh
#vim /scripts/Passwd_expire_check.sh
#01 00 * * * /scripts/Passwd_expire_check.sh
#define script content
userlist=`cat /etc/passwd |grep "/bin/bash"|awk -F ":" '{print $1}'`
count=0
sed -i '/^T/d' /etc/motd
for i in $userlist ; do
    lastupdate=`grep ^$i: /etc/shadow |awk -F: '{print $3}'`
    indate=`grep ^$i: /etc/shadow |awk -F: '{print $5}'`
    curren_days=`date +%s |awk '{print int($1/86400)}'`
        if [[ $indate == "" || $indate == 99999 ]];then
            count=`expr $count + 1`
    else
        threshold=`expr $indate + $lastupdate - $curren_days`
        if [[ 2 -le $threshold && $threshold -le 10 ]]
            then
            echo -e "The password for user \033[31m\"$i\"\033[0m will expire \033[32min\033[0m \033[31m$threshold\033[0m days,Please change !" >> /etc/motd
            #echo -e "The \033[32m$(hostname)\033[0m user \033[31m$i\033[0m password has \033[31m$threshold\033[0m expire,please change !"  |mail -s "Password alarm!" 123456@qq.com
        elif [[ $threshold -eq 1 || $threshold -eq 0 ]]
            then
            echo -e "The password for user \033[31m\"$i\"\033[0m will expire \033[32min\033[0m \033[31m$threshold\033[0m day,Please change !" >> /etc/motd
        elif [[ $threshold -lt 0 ]];then
            threshold_n=`echo $threshold|awk -F- '{print $2}'`
            echo -e "The password for user \033[31m\"$i\"\033[0m expired \033[32mout\033[0m \033[31m$threshold_n\033[0m days,Please change !" >> /etc/motd
        fi
    fi
done