Script to email Admin users with expired passwords hedkandi - Red Hat - Unix Linux Forums

  Go Back    



Script to email Admin users with expired passwords

Red Hat




Kindly Note - This is a Single User Post by Forum Member hedkandi Regarding:
Script to email Admin users with expired passwords.
Please Follow The Primary Link Above to View the Full Discussion.

   
Old 02-15-2012
hedkandi's Avatar
hedkandi hedkandi is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 10 December 2012, 5:52 AM EST
Location: Doha, Qatar
Posts: 187
Thanks: 27
Thanked 9 Times in 8 Posts
Script to email Admin users with expired passwords

Hi all

I've googled this script (to look for users with expiring passwords and email admin with the usernames) that is meant to work on Unix, and I was hoping it would run on RHEL too, but I am getting an error, below is the script, the command executed and the error which was produced. Would be glad if someone could help me find out what went wrong here. Im not very good with scripting. I am using RHEL 5.7

the script


Code:
#!/bin/sh
#
# Goran Cvetanoski - 19/12/2006
#
# pwage
#
# This script works out the time left before a password expires
#
# It will send a reminder email 10 days and 3 days before the password
# will expire. The email will go to unix.admin@mydomain.com.au unless an
# alternate email address is specified. An email will also be sent if a
# password has expired.
#
# The following command will send results to unix.admin@mydomain.com.au
# pwage oracle
#
# Specify an alternate email address if you would like the results to be
# sent to a different email address.
# ie:
# pwage oracle oracledba@mydomain.com.au
#
#
# CHANGE LOG
# =========================================================================
# 19/12/2006 - Goran Base script created
# 05/08/2009 - Ricky Smith added code to check each user
#

LOG=/tmp/pwage.log

DASHES="-----------------------------"

show()
{
    echo "$DASHES $1 $DASHES" >> $LOG
    shift
    eval "$@" >> $LOG
    echo "" >> $LOG
}

SendMail()
{
    cat $LOG | mailx -s "$1" "$2"
}

reminder ()
{

echo "Date: `date`"
echo ""
echo "Please change your password within the next $EXPIRE days"
}

expired ()
{
echo "Date: `date`"
echo ""
echo "The password for $USER has expired"
echo "$USER last changed their password on $LSTCNG"
echo "The maximum age for the password is $MAX days"
echo "and it has expired $EXPIRE days ago"
}

CheckUser()
{
        USER=$1
        EMAIL=$2

        CURRENT_EPOCH=`grep $USER /etc/shadow | cut -d: -f3`
        if [ "$CURRENT_EPOCH" = "" ]; then
                return
        fi

        # Find the epoch time since the user's password was last changed
        EPOCH=`perl -e 'print int(time/(60*60*24))'`

        # Compute the age of the user's password
        AGE=`echo $EPOCH - $CURRENT_EPOCH | bc`

        # Compute and display the number of days until password expiration
        MAX=`grep $USER /etc/shadow | cut -d: -f5`
        if [ "$MAX" = "" ]; then
                return
        fi

        EXPIRE=`echo $MAX - $AGE | bc`

        CHANGE=`echo $CURRENT_EPOCH + 1 | bc`
        LSTCNG="`perl -e 'print scalar localtime('$CHANGE' * 24 *3600);'`"

        WARN=`grep $USER /etc/shadow | cut -d: -f6`
        if [ "$WARN" = "" ]; then
                WARN=0
        fi

        if [ "$EXPIRE" -le "$WARN" ]; then
            show "R E M I N D E R" reminder
            SendMail "$USER Password Info On `uname -n`" "$EMAIL"
        elif [ "$EXPIRE" -lt 0 ]; then
            show "E X P I R E D" expired
            SendMail "WARNING: $USER Password Expired On `uname -n`" "$EMAIL"
        fi
}

# Main Code
domain=$1
if [ "$domain" = "" ]; then
        domain=$(dnsdomainname)
fi

minuid=$2
if [ "$minuid" = "" ]; then
        minuid=500
fi

IFS=':'
while read user pass uid gid full home shell
do
        if [ $uid -ge $minuid ]; then
                cat /dev/null > $LOG
                CheckUser $user "\"$full\" "
        fi
done </etc/passwd

Command run:


Code:
[root@H99A100 tmp]# ./pwage it.sysadm l@gmail.com
./pwage: line 119: [: l@gmail.com: integer expression expected
./pwage: line 119: [: l@gmail.com: integer expression expected


Last edited by hedkandi; 02-15-2012 at 02:36 AM.. Reason: removed email add