shell 密码过期检测脚本

#!/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!" [email protected]
        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

本文出自 “中庸之道” 博客,请务必保留此出处http://lynn1105.blog.51cto.com/2924741/1588005

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。