Find and alert user password expiry

Script to Alert User password Expiry before ten days of expiry date .

===================================================
#!/bin/sh

[email protected]
[email protected]
[email protected]

for i in user1 user2
do

# convert current date to seconds
currentdate=`date +%s`
# find expiration date of user
userexp=`chage -l $i |grep ‘Password Expires’ |cut -d: -f2`
# convert expiration date to seconds
passexp=`date -d “$userexp” +%s`
# find the remaining days for expiry
exp=`expr \( $passexp  – $currentdate \)`
# convert remaining days from sec to days
expday=`expr \( $exp / 86400 \)`
if [ $expday -le 10 ]; then
echo “Please do the necessary action”  | mailx -s “Password for $i will expire in $expday day/s” $rcvr3,$rcvr2
fi
done

### checking for root user password expiary ###

for j in  root
do
currentdate=`date +%s`
userexp=`chage -l $j |grep ‘Password Expires’ |cut -d: -f2`
passexp=`date -d “$userexp” +%s`
exp=`expr \( $passexp  – $currentdate \)`
expday=`expr \( $exp / 86400 \)`

if [ $expday -le 10 ]; then
echo “Please do the necessary action”  | mailx -s “Password for $j will expire in $expday day/s” $rcvr1
fi
done
===================================================

  • Gaston Berthoud

    Thank you very much, I adapted to /bin/bash on a SLES 11 server and works fine:

    #!/bin/bash

    rcvr1=mail1
    rcvr2=mail2

    for i in user
    do

    # convert current date to seconds
    currentdate=`date +%s`
    # find expiration date of user
    userexp=`chage -l $i |grep ‘Password Expires’ |cut -d: -f2`
    # convert expiration date to seconds
    passexp=`date -d “$userexp” +%s`
    # find the remaining days for expiry
    exp=`expr $passexp – $currentdate `
    # convert remaining days from sec to days
    expday=`expr $exp / 86400 `
    if [ $expday -le 10 ] ; then

    mail -r [email protected] -s “Please do the necessary action” $rcvr1 $rcvr2 <<< " Password for $i will expire in $expday day/s"
    fi
    done

  • Amit Dixit

    Hi I am using the same given script but getting the following error. Please help me

    + rcvr1=maild
    + rcvr2=mailid
    + for i in deploy
    ++ date +%s
    + currentdate=1536301800
    ++ chage -l deploy
    ++ grep $’342200230Password’ $’Expires342200231′
    ++ cut -d: -f2
    grep: Expires’: No such file or directory
    + userexp=
    ++ date -d $’342200234342200235′ +%s
    date: invalid date `“”’
    + passexp=
    ++ expr ‘(‘ $’342200223’ 1536301800 ‘)’
    expr: syntax error
    + exp=
    ++ expr ‘(‘ / 86400 ‘)’
    expr: syntax error
    + expday=
    + ‘[‘ -le 10 ‘]’
    notifype.sh: line 20: [: -le: unary operator expected