Zimbra script

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Zimbra script

Unread post by wuxmedia » Fri May 01, 2015 1:07 pm

Unless you run a large zimbra server with a password expiry policy then this won't be any damn good.
Just showing of my crappy scripting.

Code: Select all

#!/bin/bash
# WuX 2015-04-27
# Messy script for zimbra password expiry email notification.
# Meant to be performed as daily cronjob run as zimbra user. 
# redirect output to a file to get a 'log file' of sorts.

# Time taken of script;
echo "$SECONDS Started on: $(date)"

# Set some vars:
# First notification in days, then last warning:
FIRST="7"
LAST="3"

# Sent from:
FROM="[email protected]"

# Get all users - it should run once only.
USERS=$(ionice -c3 /opt/zimbra/bin/zmprov -l gaa example.com)

#Todays date, in seconds:
DATE=$(date +%s)

# Iterate through them in for loop:
for USER in $USERS
 do
# What's the password set date?
USERINFO=$(ionice -c3 /opt/zimbra/bin/zmprov ga $USER)
PASS_SET_DATE=$(echo "$USERINFO" | grep zimbraPasswordModifiedTime: | cut -d " " -f 2 | cut -c 1-8)
NAME=$(echo "$USERINFO" | grep givenName | cut -d " " -f 2)

# Make the date for expiry in 60 days from now.
EXPIRES=$(date -d  "$PASS_SET_DATE 60 days" +%s)

# Now, how many days until that?
DEADLINE=$(( (($DATE - $EXPIRES)) / -86400 ))

# Email to send to victims, ahem - users...
SUBJECT="$NAME - Your Password will expire in $DEADLINE days"
BODY="
Hi $NAME,

Your account password will expire in "$DEADLINE" days, Please reset your password soon.
You may also enter a zimbra calendar event to remind you.

Thanks, 
Admin team

"
# Send it off depending on days, adding verbose statements for the 'log'

# First warning
if [[ "$DEADLINE" -eq "$FIRST" ]]
then
	echo "Subject: $SUBJECT" "$BODY" | /opt/zimbra/postfix-2.7.5.2z/sbin/sendmail -f $FROM $USER 
	echo "Reminder email sent to: $USER - $DEADLINE days left" 
# Second
elif [[ "$DEADLINE" -eq "$LAST" ]]
then
	echo "Subject: $SUBJECT" "$BODY" | /opt/zimbra/postfix-2.7.5.2z/sbin/sendmail -f $FROM $USER
	echo "Reminder email sent to: $USER - $DEADLINE days left"
# Final
elif [[ "$DEADLINE" -eq "1" ]]
then
    echo "Subject: $SUBJECT" "$BODY" | /opt/zimbra/postfix-2.7.5.2z/sbin/sendmail -f $FROM $USER
	echo "Last chance for: $USER - $DEADLINE days left"
	
# Check for Expired accounts, get last logon date add them to EXP_LIST2
elif [[ "$DEADLINE" -lt "0" ]] && [ $(date +%d) = "01" ]
then 
    LASTDATE=$(echo "$USERINFO" | grep zimbraLastLogonTimestamp | cut -d " " -f 2 | cut -c 1-8)
    LOGON=$(date -d "$LASTDATE")
	EXP_LIST=$(echo "$USER's password has been expired for ${DEADLINE#-} day(s) now, last logon was $LOGON.")
	EXP_LIST2="$EXP_LIST2 \n $EXP_LIST"

else 
# Uncomment for very verbose logs and a list of users.
#    echo "$USER is fine, $DEADLINE days till password expiry"
fi

# Finish for loop
done

echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"

# Send off list using hardcoded email addresses. haven't worked out why the variables won't work above. 

EXP_BODY="
Hello Admin team,

This is the monthly list of expired passwords and their last recorded login date:
$(echo -e $EXP_LIST2)

Regards,
Support.
"
echo "Subject: List of accounts with expired passwords" "$EXP_BODY" | /opt/zimbra/postfix-2.7.5.2z/sbin/sendmail -f  [email protected] [email protected]
# Expired accts, for the log:
echo -e $EXP_LIST2

echo "finished in $SECONDS seconds"
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
Last edited by wuxmedia on Fri May 01, 2015 5:42 pm, edited 1 time in total.
Reason: commented a comment
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Zimbra script

Unread post by wuxmedia » Fri May 01, 2015 2:06 pm

typical - zimbra forums are not indexed by google, didn't come up in any searches.
So thought I'd post on there to pay it back: tons of fecking scripts on there, all doing the same thing :/
this is the only pure bash one though.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
doubledutch
killall X
Posts: 163
Joined: Sun Aug 10, 2014 1:25 pm

Re: Zimbra script

Unread post by doubledutch » Fri May 01, 2015 2:53 pm

Comment out the line "Get all users - it should run once only."

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Zimbra script

Unread post by wuxmedia » Fri May 01, 2015 5:44 pm

Well spotted - thanks!
must have got lost in the c/p to the forum.
* rushes to tmux session to check prod version :)
yeah we're good :D
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Zimbra script

Unread post by wuxmedia » Wed Jun 03, 2015 12:38 pm

For those of you who are interested version that works better is on github.
(you'd think i'd have got the hang of quotes by now)
bash scripting problems = usually quotes or a subshell
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Zimbra script

Unread post by machinebacon » Thu Jun 04, 2015 2:46 am

/moving this over to /usr/local/bin because it's actually a script, not a config :)
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Zimbra script

Unread post by wuxmedia » Thu Jun 04, 2015 7:30 am

What the hell possessed me to put it in .config ?
anyway, thanks for moving :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Zimbra script

Unread post by machinebacon » Thu Jun 04, 2015 9:03 am

^ guess with the new forums names it will be easier ;)
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Zimbra script

Unread post by wuxmedia » Thu Jun 04, 2015 11:03 am

Oh yeah, VERY NICE AND CLEAR :p
(seriously it works for me very well for me!)
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Zimbra script

Unread post by machinebacon » Thu Jun 04, 2015 11:33 am

^ THOUGHT SO! :D
..gnutella..

Post Reply