I put together this little script, run from cron,
to email my users when it's time to change their password. Feel
free to use and chop up if you like..
#!/usr/bin/perl
# Name: check_password_expiration_date.pl
# Author: Leo
Pleiman
# Date: July 11,
2013
# This is designed
as a cron job to query the password expiration date on
accounts and send an email
# of the impending
expiration to the account owner and the Systems Admins
use Date::Calc qw(
Delta_Days Today );
use Mail::Sendmail;
$cmd =
"\/usr\/bin\/ldapsearch -x passwordexpirationtime=*|grep
uid:|grep -v apple|cut -c6- >/tmp/users\n";
system $cmd;
open USERS,
"/tmp/users" or die "Couldn't open file";
while
(<USERS>) {
chomp;
$user = $_;
$USER = uc($user);
$cmd =
"\/usr\/bin\/ldapsearch -x uid=$user +
passwordexpirationtime|grep passwordexpirationtime:|cut
-d: -f2|cut -c2-9 >/tmp/date\n";
system $cmd;
system "cat
/tmp/date|cut -c1-4 >/tmp/year";
system "cat
/tmp/date|cut -c5-6 >/tmp/month";
system "cat
/tmp/date|cut -c7-8 >/tmp/day";
open FILE,
"/tmp/year" or die "Couldn't open file";
while
(<FILE>) {
chomp;
$YEAR = $_;
close FILE;
}
open FILE,
"/tmp/month" or die "Couldn't open file";
while
(<FILE>) {
chomp;
$MONTH = $_;
close FILE;
}
open FILE,
"/tmp/day" or die "Couldn't open file";
while
(<FILE>) {
chomp;
$DAY = $_;
close FILE;
}
($YEAR2,$MONTH2,$DAY2)
= Today();
$days =
Delta_Days($YEAR2,$MONTH2,$DAY2, $YEAR,$MONTH,$DAY);
$DAYS = $days - 7;
#print "$days
$DAYS $USER\n";
if ($DAYS <= 7
) {
if ($DAYS <= 0
) {
%mail = ( To
=> "$user\@example.com",
Cc => 'systemsgroup@xxxxxxxxxxx',
From => 'systemsgroup@xxxxxxxxxxx',
Subject => "$USER YOUR PASSWORD HAS EXPIRED",
Message => "$user, your password has expired.
You have a limited number of logins avaliable before your
account will be locked. Proceed immediately to blah, blah,
blah\n sysref $days"
);
sendmail(%mail) or die $Mail::Sendmail::error;
}
else {
%mail = ( To
=> "$user\@example.com",
From => 'systemsgroup@xxxxxxxxxxx',
Subject => "$USER YOUR PASSWORD IS ABOUT TO
EXPIRE",
Message => "$user, your password will expire in
$DAYS days. Reference blah, blah, blah instructions on
changing your password."
);
sendmail(%mail) or
die $Mail::Sendmail::error;
# print "OK. Log
says:\n", $Mail::Sendmail::log;
}
}
}
close USERS;
system "cd /tmp;rm
-rf users date year month day";
exit