<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "[Linux]如何讓Linux作業系統帳號之密碼自逾期7天前起發送通知信"]]></title>
		<link>https://forum.andowson.com/posts/list/16.page</link>
		<description><![CDATA[Latest messages posted in the topic "[Linux]如何讓Linux作業系統帳號之密碼自逾期7天前起發送通知信"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>[Linux]如何讓Linux作業系統帳號之密碼自逾期7天前起發送通知信</title>
				<description><![CDATA[ 很多公司的密碼政策都會要求定期更新一次密碼(例如90天)，對於作業系統的帳號，需要使用者要登入時才能看到系統的提示訊息，告知其帳號還有幾天將要逾期。如果使用者都不登入作業系統，則該訊息將不會被使用者看到，很容易造成該使用者要用時才發現無法正常登入。 
<br>
<br>
針對這個需求，我們可以透過一支發送通知信的shell script，每天定期掃一下系統的帳號檔/etc/passwd，搭配chage -l 指令找出即將（及已經）逾期的帳號有哪些，並個別發送信件給他們。底下參考的內容： 
<br>
<br>
/root/admin/pwd_expire_notice.sh: 
<br>
[code=bash]#!/bin/bash 
<br>
# Name: pwd_expire_notice.sh 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 1.0 
<br>
# Last Modified: 2011-06-22 
<br>
<br>
noticefile=/tmp/pwd_expire.txt 
<br>
ip=`/sbin/ifconfig eth0|grep "inet addr"|awk '{print $2}'|cut -d":" -f2` 
<br>
echo $ip 
<br>
D1=`date +%s` 
<br>
for i in `cat /etc/passwd | grep bash | awk -F: '{print $1}'` 
<br>
do 
<br>
 export LANG=en_US 
<br>
 echo -n "password for user ${i} will expire in " 
<br>
 exp_date=`chage -l $i|grep "Password [Ee]xpires"|awk -F: '{print $2}'` 
<br>
 D2=`date +%s -d"${exp_date}"` 
<br>
 ((diff_sec=D2-D1)) 
<br>
 daysleft=`echo - | awk -v SECS=$diff_sec '{printf "%d", SECS/(60*60*24)}'` 
<br>
 echo "$daysleft days" 
<br>
 if [ $daysleft -le 7 ]; then 
<br>
 echo "$i's password will expire in $daysleft days." &gt; $noticefile 
<br>
 mailto=`grep $i /root/admin/mail.txt|awk -F: '{print $2}'` 
<br>
 if [ -n "$mailto" ]; then 
<br>
 echo "Send mail to ${mailto}" 
<br>
 export LANG=zh_TW.Big5 
<br>
 mail -s "密碼即將到期通知： $i@$ip 尚餘 $daysleft 日" $mailto &lt; $noticefile 
<br>
 fi 
<br>
 fi 
<br>
done[/code] 
<br>
<br>
說明： 
<br>
1.我們利用 chage -l username 可以找出全部的密碼相關日期資訊，利用grep指令過濾岀Password Expires(CentOS 4.x之前)或Password expires（CentOS 5.x)後面的日期字串， 
<br>
2.接著再算出系統日期跟這個密碼到期日之間的天數差異 
<br>
3.如果天數差異小於或等於7天，再比對mail.txt檔中有設定電子郵件的，我們就發送通知信給他 
<br>
<br>
作業系統帳號和電子郵件信箱的對應檔（可針對想要收到通知信的帳號一一設定，格式為帳號:電子郵件，中間用一個冒號隔開不加空白） 
<br>
/root/admin/mail.txt: 
<br>
[code]root:sys.admin@example.com 
<br>
john:john.doe@example.com[/code] 
<br>
<br>
設定排程自動執行 
<br>
/etc/crontab: 
<br>
[code]# Notice password will expire (2011.06.22 by Andowson) 
<br>
10 5 * * * root /root/admin/pwd_expire_notice.sh &gt; /var/log/pwd_expire_notice.log 2&gt;&amp;1[/code] 
<br>
<br>
注意： 
<br>
您必須已經先設定好該部Linux主機可以發送電子郵件到您公司的信箱才行。例如安裝並設定Sendmail。 
<br>
<br>
參考資料: 
<br>
http://www.planetmy.com/blog/how-to-show-linux-user-password-expire/ 
<br>
http://www.unix.com/shell-programming-scripting/56451-days-difference-between-two-dates.html]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/539/1066.page</guid>
				<link>https://forum.andowson.com/posts/preList/539/1066.page</link>
				<pubDate><![CDATA[Wed, 22 Jun 2011 17:44:35]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>