Script to send an email for password expiry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to send an email for password expiry
# 1  
Old 07-08-2015
Script to send an email for password expiry

Newbie in scripting

Please assist with a script to send an email to all users seven days before their passwords expires.Aging set for 90 days.

Code:
[root@redhat ~]# chage -l user1
Last password change                                    : Jul 08, 2015
Password expires                                        : Oct 06, 2015
Password inactive                                       : never
Account expires                                         : never
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


Last edited by Corona688; 07-08-2015 at 12:00 PM..
# 2  
Old 07-08-2015
What have you tried?

I would use something which is run by cron, once a day, to check all relevant accounts and email if the expiry is exactly that far away.
# 3  
Old 07-08-2015
Below is what I wrote and I am not winning

Code:
cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
totalaccounts=`cat /tmp/expirelist.txt | wc -l`
for((i=1; i<=$totalaccounts; i++ ))
       do
       tuserval=`head -n $i /tmp/expirelist.txt | tail -n 1`
       username=`echo $tuserval | cut -f1 -d:`
       userexp=`echo $tuserval | cut -f2 -d:`
       userexpireinseconds=$(( $userexp * 86400 ))
       todaystime=`date +%s`
       #check if the user expired or not?
       if [ $userexpireinseconds -ge $todaystime ] ;
           then
           timeto7days=$(( $todaystime + 604800 ))
                if [ $userexpireinseconds -le $timeto7days ];
                then
                mail -s "The account $username will expire less than 7 days" root
                fi
       else
       mail -s "The user account $username already expired" root
       fi
done

Moderator's Comments:
Mod Comment Please wrap all code, files, input & output/errors in CODE tags.
it makes them far easier to read and preserves multiple spaces which can be critical for indenting or fixed width data.

Last edited by rbatte1; 07-08-2015 at 01:13 PM.. Reason: Add CODE tags
# 4  
Old 07-08-2015
What problem are you having with it?
# 5  
Old 07-08-2015
Please note that field 8 in /etc/shadow is the "account expiration". From man shadow:
Quote:
account expiration date
The date of expiration of the account, expressed as the number of days since Jan 1, 1970.

Note that an account expiration differs from a password expiration. In case of an acount expiration, the user shall not be allowed to login. In case of a password
expiration, the user is not allowed to login using her password.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Password expiry send to individual user

Hi Folks, I am having a script which will send out the password expiry email notification with just the username to a common group id, but I am looking for a change in like: the password expiry email notification should send to an individual user account with their email id including the... (1 Reply)
Discussion started by: gsiva
1 Replies

2. Red Hat

Root password expiry script

Hi Guys In red hat linux server is there a way to alert via email when the root password is about to expire ? As per security policy in our environment root password will expire in 90 days. Example : It would be better if we receive a email on 7th november stating that the root password... (1 Reply)
Discussion started by: newtoaixos
1 Replies

3. Shell Programming and Scripting

How to get rid off Password expiry error message when connecting to sql in script?

I am connecting to sql databases through shell script. Databases that i am connecting will need password change every 60 days. This is according to our security policy and cannot be changed. But this is creating problem when connecting to Databases through shell script . To connect to oracle DB we... (2 Replies)
Discussion started by: pallvi_mahajan
2 Replies

4. Shell Programming and Scripting

Need a script to set non-expiry password

Hi All, I have a gateway server, from where I can connect any server via 'sudo ssh server_name'. Generally, if we need to run a command on any number of servers, we put server names in a file like '/tmp/ser_name' and execute it with for loop from gateway server. Now, I need to set non-expiry... (0 Replies)
Discussion started by: solaris_1977
0 Replies

5. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

6. Solaris

disable password expiry

Hi How do i disable password expiration on ldap? It runs on Solaris 10 machine. Thanks in advance. (3 Replies)
Discussion started by: hrist
3 Replies

7. Solaris

SSH Password-less login fails on password expiry.

Hi Gurus I have a few Sol 5.9 servers and i have enabled password less authentication between them for my user ID. Often i have found that when my password has expired,the login fails. Resetting my password reenables the keys. Do i need to do something to avoid this scenario or is this... (2 Replies)
Discussion started by: Renjesh
2 Replies

8. Solaris

Notification of password expiry.

Hi, Is there any way of sending an email to a number of users indicating that the passwords of user accounts will expire? Currently we have a test server with a number of oracle test accounts on it. Each of these accounts correspond to an instance of Oracle on the server. These... (2 Replies)
Discussion started by: sparcman
2 Replies

9. Shell Programming and Scripting

Need to write script to send an email for password reset.

Hi all, Please suggest I want to write a scritp which will send a email to my mail address before the time duration of the password reset, Should write mail stating that your password will expire in so and so days"" Rgds:b: Ann. (2 Replies)
Discussion started by: Haque123
2 Replies

10. UNIX for Advanced & Expert Users

password expiry

Hi, under SUN Unix, in which file the expiry date of a user password is indicated ? Many thanks. (2 Replies)
Discussion started by: big123456
2 Replies
Login or Register to Ask a Question