Problem with Script to email Admin users with expired passwords writed byygemici

Hi,
I have problem with a script, it was working for 6 month and suddenly I started getting strange expire times
example:

[root@XXXXX passexp]# chage -l wXXp
Last password change                                    : Oct 28, 2014
Password expires                                        : Nov 27, 2014
Password inactive                                       : Nov 27, 2014
Account expires                                         : Nov 27, 2014
Minimum number of days between password change          : 0
Maximum number of days between password change          : 30
Number of days of warning before password expires       : 1
[root@XXXXX passexp]# ./passexp.sh wXXp root@XXXXX.localdomain 1
date: option requires an argument -- 'd'
Try `date --help' for more information.
Password Expired for wXXp user for '-16373' day(s).

And the script:

# cat check_passwd_expiry.sh
#!/bin/bash
## @ygemici unix.com simple password expire check script
## e.g -> "./passwordcheck.sh 'user' mailaddress" 'IFEXPDAYS[optional]'
## IFEXPDAYS if you specify interval day(s) for send to mail
## for ex -> IFEXPDAYS=100 means compare expire days if is it [1-100] then it sends e-mail
param_check() {
id $1 &>/dev/null
if [ ! $? -eq 0 ]; then
echo "No such user" ; exit 1;fi
if [[ ! $(echo "$2"|grep -o '.*@.*\..*') ]];then
echo "Check your e-mail address!!";exit 1;fi
}
param_check $1 $2
check() {
expirestatus=$(chage -l $1|awk -F'[: ]' '/Password expires/{print $4}')
[[ "$expirestatus" == "never" ]] && echo -e "Password expire status is NEVER for '$1' user.\nNo Send to Mail!!"&&exit
}
check $1
doit() {
expiretime=$(date '+%s' -d $(chage -l "$1"|\
awk -F: '/Password expires/{print $2}'|\
awk 'BEGIN{ORS="=";;m="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Dec"};
{sub(",","",$2);split(m,a);for(i=1;i<=12;i++)if(a==$1)printf "%02s/%s/%s",i,$2,$3}'))
expl=$(awk -vnow=$(date '+%s') -vexpt=$expiretime 'BEGIN{print int((expt-now)/60/60/24)}')
mailaddr="$2"
if [[ $(echo "$expl"|grep '-') ]]; then
message="Password Expired for $1 user for '$expl' day(s)"
echo -e "$message.\nAn e-mail sent to '$2' for this information."
echo "Haslo wygasa za  '$expl' dni dla uzytkownika '$1'."|mail -s "Password Expired" "$mailaddr"
exit;fi
if [ ! -z "$3" ]; then
awk -ve=$expl -vi=$3 'BEGIN{if(e<i&&e>1){
print "An e-mail (Password Expiration WARNING!!) sent to ( '$mailaddr' )..";
system("echo \"Please change your password within the next in '$expl' days\" |mail -s \"Password Expire Notification\" '$mailaddr'")}}'
else
echo -e "Haslo wygasa za  '$expl' dni dla uzytkownika '$1'.\nNo Send to Mail!!"
fi
}
doit $1 "$2" $3

Thx for help

There's two occurrences of date in your spaghetti above. One has the -d option. The error message indicates that the result of $(chage ... is empty. Try to analyze that command and its results - I can't.

November is missing!

Try changing this

awk 'BEGIN{ORS="=";;m="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Dec"};

to this

awk 'BEGIN{ORS="=";;m="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"};
1 Like

Thx for help I was looking at that script for two days and didn't saw it...