Tuesday, 22 October 2013

Script to Generate Alert for Root User Password Expiration


Here i created a script that will check the expiry of root user and generate the alert to system administrator through mail since one month ago of password expiration. Here i considered 90 days of expiration of root user and alert will be generated after 60 days. I mentioned my mail id here in place of system admin mail id. You can specify your mail id in variable RECIP.

# vi  root_expir_alert

#!/bin/bash

HOST=`uname -n`
USER=`id -u`
RECIP="jitendrakumaryogi@gmail.com"
MAX=60
SYS=`uname`

#--- Need root permissions to run script

if [[ $USER -ne 0 ]];
           then
    echo "Must be root to run this script!!"
else 
                exit 2
   fi

#--- Number of Days since 1st Jan 1970 of root Password Change
DAYS=`grep $USER /etc/shadow | cut -d: -f3`

#--- Number of Days since 1st Jan 1970 to till date
DATE=`perl -e 'print int(time/(60*60*24))'`

#--- Compute the Age of the user's password
AGE=`echo $DATE - $DAYS | bc`

NOTIFY="The user's password is $AGE days old on $HOST"

if [[ $SYS == Linux ]];
   then
DAYS=`grep $USER /etc/shadow | cut -d: -f3`
fi

#--- If User expiry is 90 Days Alert will be generated 30 Days ago and mailed to user.

if [[ $AGE -ge $MAX ]];
   then
WARN=`echo 90 - $AGE | bc`
echo $NOTIFY | mail -s "Root Password will expire in $WARN days"  $RECIP

fi



Now you can set this script in crontab daily on particular time. I am scheduling this script daily on 12:00 AM.

# crontab -e

00 00 * * *  /scripts/root_expir_alert


3 comments:

  1. Im getting an error ...

    -bash-4.1# ./root_expir_alert
    (standard_in) 1: syntax error
    -bash-4.1#

    ReplyDelete
    Replies
    1. Hi Abdul,

      There was minor error, i have resolved it.

      [root@syncsrv01 jitendrakumar]# vi root_expir_alert
      [root@syncsrv01 jitendrakumar]# ./root_expir_alert
      [root@syncsrv01 jitendrakumar]#

      Delete
  2. This comment has been removed by the author.

    ReplyDelete