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

时间:2015-10-06 15:10:06

标签: linux bash email cron

我在Debian 7.8上运行了tacacs +服务器。我使用apt-get install tacacs +来安装tacacs +,所以没什么特别的。我使用tacacs +来验证Cisco VPN机器中的用户。

我的问题是,我无法找到任何为tacacs +开发的内容,以便在密码过期之前向用户/服务器管理员发送电子邮件。有可能要求用户在登录时更改密码,但如果帐户已经过期,则无法正常工作。

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

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

请您提供一个脚本,可以比较" expires"中的日期。使用系统日期,如果在系统日期=到期之前剩余不到14天,则发送自动电子邮件到特定地址(例如admin@domain.local)并发出警告消息(例如" tacacs + /示例的Cisco VPN帐户将在X天后过期")?

提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

@ 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 $0":";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 $1}')
        #getting the expiration date coresponding to the user
        date_expire=$(echo $line | awk -F: '{print $2}' | 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