shell 密碼到期檢測指令碼

來源:互聯網
上載者:User

標籤: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

shell 密碼到期檢測指令碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.