From 27c2842cd216b0b65b29047f4880054e4cac5a85 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Tue, 28 Aug 2018 21:16:20 +0100 Subject: [PATCH] remove duplication of sql, remove use of functions (hopefully unnecessary) --- check_mailpass_expiration.sh | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/check_mailpass_expiration.sh b/check_mailpass_expiration.sh index 44bf6906..a7b33728 100644 --- a/check_mailpass_expiration.sh +++ b/check_mailpass_expiration.sh @@ -4,26 +4,17 @@ POSTFIX_DB="postfix_test" MYSQL_CREDENTIALS_FILE="postfixadmin.my.cnf" -#All the rest should be OK -QUERY30DAYS="SELECT username,pw_expires_on FROM mailbox WHERE pw_expires_on > now() + interval 29 DAY AND pw_expires_on < now() + interval 30 day" -QUERY14DAYS="SELECT username,pw_expires_on FROM mailbox WHERE pw_expires_on > now() + interval 13 DAY AND pw_expires_on < now() + interval 14 day" -QUERY7DAYS="SELECT username,pw_expires_on FROM mailbox WHERE pw_expires_on > now() + interval 6 DAY AND pw_expires_on < now() + interval 7 day" +REPLY_ADDRESS=noreply@example.com -function notifyThirtyDays() { - mysql --defaults-extra-file="$MYSQL_CREDENTIALS_FILE" "$POSTFIX_DB" -B -e "$QUERY30DAYS" | while read -a RESULT ; do - echo -e "Dear User, \n Your password will expire on ${RESULT[1]}" | mail -s "Password 30 days before expiration notication" -r noreply@eyetech.fr ${RESULT[0]} ; done -} +# Change this list to change notification times and when ... +for INTERVAL in 30 14 7 +do + LOWER=$(( $INTERVAL - 1 )) -function notifyFourteenDays() { - mysql --defaults-extra-file="$MYSQL_CREDENTIALS_FILE" "$POSTFIX_DB" -B -e "$QUERY14DAYS" | while read -a RESULT ; do - echo -e "Dear User, \n Your password will expire on ${RESULT[1]}" | mail -s "Password 14 days before expiration notication" -r noreply@eyetech.fr ${RESULT[0]} ; done -} + QUERY="SELECT username,password_expiry FROM mailbox WHERE password_expiry > now() + interval $LOWER DAY AND password_expiry < NOW() + interval $INTERVAL DAY" -function notifySevenDays() { - mysql --defaults-extra-file="$MYSQL_CREDENTIALS_FILE" "$POSTFIX_DB" -B -e "$QUERY7DAYS" | while read -a RESULT ; do - echo -e "Dear User, \n Your password will expire on ${RESULT[1]}" | mail -s "Password 7 days before expiraiton notication" -r noreply@eyetech.fr ${RESULT[0]} ; done -} + mysql --defaults-extra-file="$MYSQL_CREDENTIALS_FILE" "$POSTFIX_DB" -B -e "$QUERY" | while read -a RESULT ; do + echo -e "Dear User, \n Your password will expire on ${RESULT[1]}" | mail -s "Password 30 days before expiration notication" -r $REPLY_ADDRESS ${RESULT[0]} + done -notifyThirtyDays # Execute the function for 30 day notices -notifyFourteenDays # Execute the function for 14 day notices -notifySevenDays # Execute the function for 7 day notices +done