Linux - 将系统日期与文件中的日期进行比较并发送电子邮件给管理员

Linux - compare system date with date in file and send e-mail to admin

我在 Debian 7.8 上有一个 tacacs+ 服务器 运行。我使用 apt-get install tacacs+ 来安装 tacacs+,所以这里没什么特别的。我使用 tacacs+ 对 Cisco VPN 机器中的用户进行身份验证。

我的问题是我找不到任何已经为 tacacs+ 开发的东西,可以在 users/server 管理员密码过期之前向他们发送电子邮件。有可能要求用户在登录时更改密码,但如果帐户已经过期,则不起作用。

下面是 tac_plus.conf 文件中用户帐户设置的示例。

user = example.user {
default service = permit
login = des some_crypted_pass_here
expires = "Sep 30 2016"
}

能否请您提供一个脚本,可以将 "expires" 中的日期与系统日期进行比较,如果距离系统日期 = 过期还剩不到 14 天,则发送一封自动电子邮件到带有警告消息(例如 "The tacacs+/Cisco VPN account for example.user will expire in X days" )的特定地址(例如 admin@domain.local ) ?

提前谢谢大家。

@John1024 在听从你的建议后,我想到了这个:

#!/bin/bash

#location of temporary file to store the results
temp_file=/usr/local/src/temp_file_tacacs 

#grep | cut | awk in the file /etc/tacacs+/tac_plus.conf and exporting resuts to temporary file
grep -e "user\|expires" /etc/tacacs+/tac_plus.conf | cut -d'=' -f 2 | tr -d { | tr -d \" | awk 'NR%2{printf [=10=]":";next;}1' > $temp_file 

#Getting current system date 
date_current=$(date "+%s")  

while IFS='' read -r line || [[ -n "$line" ]]; do
        #getting the user 
        user=$(echo $line | awk '{print }')
        #getting the expiration date coresponding to the user
        date_expire=$(echo $line | awk -F: '{print }' | xargs) 
        #converting the date in the same format as $date_current ( was retrieved earlier )
        date_converted=$(date -d "$date_expire" +"%s")
        #calculating the difference between the 2 dates
        date_diff=$(expr $date_converted - $date_current)

        #checking if result is < 14 days ( 1209600 seconds = 14 days )
        if [ $(expr $date_diff - 1209600) -gt 0 ] 
        then
                #echo "$user - more than 14 days untill account will expire."  #left here for debugging purposes

                #ignoring there results and sending them to /dev/null ( just because I needed something in the while - then - else loop). 
                echo "" > /dev/null  
        else
                #echoing the results and sending them with with sendmail
                #If sendmail is not sending, check your /var/log/exim4/mailnlog.
                #I had to dpkg-reconfigure exim4-config and select "internet sites" option to be able to send e-mails to remote domains. Check
                # http://chepri.com/jake-strawn-fixed-mailing-to-remote-domains-not-supported-debian-5-lenny/ for more info about this 

                #NOTE: For each result, you will get a  different e-mail. If too many accounts are about to expire, you may be blocked by the mail server for spamming!!

                echo -e "Hello Admins,\n\nThe $user Cisco VPN Account will expire in less than 14 days!" | /usr/bin/mail -s "The Cisco VPN Account for $user is about to expire" "email@domain.com" 

        #END of the while loop  
        fi

done < "$temp_file"

#deleting the temporary file
rm -rf $temp_file