Link to home
Start Free TrialLog in
Avatar of RITU Raj
RITU Raj

asked on

Password expiry email notification

Hi Team,

I am having a requirement to write a shell script for password expiry along with email notification for Linux users.
Request you to do this needful.
SOLUTION
Avatar of Prabhin MP
Prabhin MP
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RITU Raj
RITU Raj

ASKER

Hi Prabhin,

Thanks for the prompt response.
Please help me with below details.-
Each user needs a $HOME/.forward file that contains a valid
#   email address.


[root@xibo ~]# chage -l test
Last password change                                    : Apr 29, 2018
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : May 02, 2018
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
[root@xibo ~]# perl ./Password_Exp_Notify.pl
[root@xibo ~]#

[root@xibo test]# cd /home/
[root@xibo home]# ls -ld *
drwx------. 2 ritu ritu    59 Apr 29 10:01 ritu
drwx------. 2 test test    59 Apr 29 16:48 test
i have created .forward like below
[root@xibo ~]# cat /home/test/.forward
rituraj.s@247-inc.com
[root@xibo ~]#

but still not getting email alert.Please check
[root@xibo ~]# ls -ld /home/test/.forward
-rwxrwxr-x. 1 root root 22 Apr 29 17:36 /home/test/.forward
[root@xibo ~]#
change the owenership as well but no go.
[root@smg-nagios home]# ls -ld /home/test/.forward
-rwxrwxr-x 1 test test 17 Apr 30 18:51 /home/test/.forward

[root@smg-nagios home]#
Hi,
Change the file permission to 400.
Chmod 400 . forward



Hope this will work
I have tried but still same

[root@smg-nagios home]# chmod 400 /home/test/.forward
[root@smg-nagios home]# perl Password_Exp_Notify.pl
[root@smg-nagios home]# chage -l o.siva
Last password change                                    : Mar 21, 2018
Password expires                                        : Jun 19, 2018
Password inactive                                       : never
Account expires                                         : May 02, 2018
Minimum number of days between password change          : 0
Maximum number of days between password change          : 90
Number of days of warning before password expires       : 7
[root@smg-nagios home]# ls -ld /home/o.siva/.forward
-r-------- 1 o.siva o.siva 22 Apr 30 18:43 /home/o.siva/.forward
[root@smg-nagios home]# cat /home/o.siva/.forward
rituraj.s@247-inc.com
[root@smg-nagios home]# mailq
Mail queue is empty
[root@smg-nagios home]#
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Prabhin your help.
There was some dependency issue hence couldn't able to execute the script which you have provided.
However, the requirement is successfully completed by using below script.

#!/bin/bash
# As bash script to find any FTP accounts (except ftp), and reports to that account's owner in email if password account expiery date in within 60 days frame.

Fcount=0
NFcount=0
#echo -e "\n"
#hostname -s
#echo "=========="
ftpusers="rituraj"
for i in $ftpusers
do
        checkuser=`grep -w $i /etc/passwd`
        if [ ! -z "$checkuser" ]; then
                Fcount=`expr $Fcount + 1`
                #echo "INFO-**This FTP account [$i] exists on this host.**"
                user_email=`echo $checkuser|sed -r -e "s/^.*[[:space:],:]([^,:]+@[^,:[:space:]]+).*$/\1/g"`
                #echo "User > $i & Email > $user_email"
                EXPDATE=`chage -l $i | grep "Password expires" | awk -F ":" '{ print $2 }' | sed -e 's/^[ \t]*//' `
                if [ "$EXPDATE" != "never" ]; then
                        EXPDATE_NEWFORMAT=`date --d="$EXPDATE" +%F`
                        EXPDATE_INSEC=`date --date="$EXPDATE_NEWFORMAT" +%s`
                        todayinsec=`date +%s`
                        days=`echo "($EXPDATE_INSEC - $todayinsec)/86400"|bc`
                        if [ $days -le 60 ]; then
                                #echo "User [$i] >> Password reset required for this account. Email will be sent to [$user_email]"
                                echo "User [$i] >> Password reset required for this account." | mail -s "Account [$i] password reset required on [`hostname -s`]" $user_email -c rajritu45@gmail.com
                        fi
                fi
        else
                #echo "This FTP account [$i] does not exists on this host."
                NFcount=`expr $NFcount + 1`
        fi
done
#echo "Total Fount :- $Fcount"
#echo "Total Not fount :- $NFcount"
Script is working as per the requirements