#!/usr/bin/perl -w
use Date::Calc qw/Delta_Days/;
use Net::SMTP;
use Net::SMTP_auth;
use MIME::Base64;
@userlist = `cat /etc/passwd|awk -F: '{if(\$3>=500&&\$3<=1000)print \$1}'`;
foreach (@userlist) {
$username = "$_";
chomp($username);
print "User processed is $username", "\n";
$expireStr = `chage -l $username |awk -F':' '/Account/{print \$2}'`;
userDateLeft($expireStr);
}
if ( $mailContent ) {
print "The mail body is: ", "$mailContent", "\n";
MailtoSmtp($mailContent);
}
sub userDateLeft {
$dateexpire = $expireStr;
$dateexpire =~ s/^\s+//;
chomp($dateexpire);
$monstring = (split(' ', $dateexpire))[0];
if ( $monstring ne "never" ) {
$monstring = (split(' ', $dateexpire))[0];
$chdaystring = (split(',', (split(' ', $dateexpire))[1]))[0];
$yearstring = (split(' ', $dateexpire))[2];
#$wholedate1 = "$monstring\-$chdaystring\-$yearstring";
%months = (
Jan => '01', Feb => '02', Mar => '03', Apr => '04',
May => '05', Jun => '06', Jul => '07', Aug => '08',
Sep => '09', Oct => '10', Nov => '11', Dec => '12',
);
$Digmon = "$months{$monstring}";
$wholedate2 = "$Digmon\-$chdaystring\-$yearstring";
print "The whole date is $wholedate2", "\n";
print "The month is $Digmon", "\n";
$oldYear = $yearstring;
$oldMon = $Digmon;
$oldDate = $chdaystring;
$currYear = `date +%Y`;
$currMon = `date +%m`;
$currDate = `date +%d`;
$daysDif = Delta_Days( $currYear, $currMon, $currDate, $oldYear, $oldMon, $oldDate);
if ( $daysDif <= 10) {
$mailContent .= "The $username account has $daysDif days left\n";
}
#print "The mail body is: ", "$mailContent", "\n";
}
}
sub MailtoSmtp {
$mail_server = 'smtp.example.com';
$mail_from = 'user1@example.com';
$mail_to = 'user2@example.com';
$uname = 'user1';
$passwd = 'password';
$mailtitle = 'account availability';
# Debug Mode
$smtp = Net::SMTP->new("$mail_server" , Debug => 1);
# Normal Mode
#$smtp = Net::SMTP->new("$mail_server" );
$smtp->auth("$uname", "$password");
$smtp->mail("$mail_from");
$smtp->to("$mail_to");
$smtp->data();
$smtp->datasend("Content-Type:text/plain;charset=GB2312\n");
$smtp->datasend("Content-Transfer-Encoding:base64\n");
$smtp->datasend("From:$mail_from \n");
$smtp->datasend("To:$mail_to \n");
$smtp->datasend("Subject:=?gb2312?B?".encode_base64($mailtitle,'')."?=\n\n");
$smtp->datasend("\n");
$smtp->datasend(encode_base64($mailContent,'')." \n");
$smtp->dataend();
$smtp->quit;
}
|