码迷,mamicode.com
首页 > 系统相关 > 详细

shell 密码过期检测脚本

时间:2014-12-09 19:53:25      阅读:164      评论:0      收藏:0      [点我收藏+]

标签: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!" 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

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

shell 密码过期检测脚本

标签:shell 用户密码过期检测

原文地址:http://lynn1105.blog.51cto.com/2924741/1588005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!