gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:49 24 4
gpt4 key购买 nike

我有一个在 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"
}

您能否提供一个脚本,可以将“过期”中的日期与系统日期进行比较,如果距离系统日期 = 过期还剩不到 14 天,则向特定地址(例如admin@domain.local ) 和一条警告消息(例如“The tacacs+/Cisco VPN account for example.user will expire in X days”)?

提前谢谢大家。

最佳答案

@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

关于Linux - 将系统日期与文件中的日期进行比较并发送电子邮件给管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32973449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com