Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user expire script

Status
Not open for further replies.

THEaix

MIS
Jun 7, 2007
30
US
I found this thread from TEK-TIP user Columb. I am real new to scripting and perl,(just figured how to spell it) and was wanting a little help in understanding this script and modfying it to not mail the users but mail me.
Here is the script:

#! /usr/bin/perl -w
use strict;

open FH, "/etc/passwd";
while (<FH>)
{
my ( $uname, undef, $uid ) = split /:/;
$uid > 100 and push @helpdesk, $uname;
}
close FH;
my @helpdesk = qw ;

my $exptime = 56
my $warntime = 42
my $secs_in_day = 60 * 60 * 24;
my $now = int ((time)/($secs_in_day));
my $host = `uname -n`;
chomp $host;

foreach my $user ( @helpdesk )
{
system "lsuser $user >/dev/null 2>&1" and (print STDERR "$user not on system\n
"), next;
my $chtime = `lssec -f /etc/security/passwd -s $user -a lastupdate`;
$chtime =~ s/^.*?=(\d+)/$1/;
$chtime or (print STDERR "$user has nver been updated\n"), next;
$chtime = int ($chtime/$secs_in_day);
$chtime = $now - $chtime;
$chtime < $warntime and next;
$chtime > $exptime and (system "echo Your password on $host has expired - Contact Unix support to reset | mail -s \"Password on $host\" \" $user\@mailserver"), next;
$chtime = 56 - $chtime;
system "echo Your password on $host will expire in $chtime days - please log i
n and reset it | mail -s \"Password on $host\" $user\@mailserver";
}


Thanks to all that take there time to help. I know that TIME is one of those valuable commodities that get taken advantage of!

 
Hi THEaix,

I'm not at all a Perl programmer but i was waiting for someone to help you and you seems to be desperate so i thought of contributing into this theard :)

To be able to modify this script i had to read how to declare new variables in Perl using this link:


Code:
#! /usr/bin/perl -w
use strict;

open FH, "/etc/passwd";
while (<FH>)
{
my ( $uname, undef, $uid ) = split /:/;
$uid > 100 and push @helpdesk, $uname;
}
close FH;
my @helpdesk = qw ;

my $exptime = 56
my $warntime = 42
my $secs_in_day = 60 * 60 * 24;
my $now = int ((time)/($secs_in_day));
my $host = `uname -n`;
my $email_user_name = 'khalid'
chomp $host;

foreach my $user ( @helpdesk )
{
system "lsuser $user >/dev/null 2>&1" and (print STDERR "$user not on system\n
"), next;
my $chtime = `lssec -f /etc/security/passwd -s $user -a lastupdate`;
$chtime =~ s/^.*?=(\d+)/$1/;
$chtime or (print STDERR "$user has nver been updated\n"), next;
$chtime = int ($chtime/$secs_in_day);
$chtime = $now - $chtime;
$chtime < $warntime and next;
$chtime > $exptime and (system "echo $user password on $host has expired - Contact Unix support to reset | mail -s \"Password on $host\" \" $email_user_name\@mailserver"), next;
$chtime = 56 - $chtime;
system "echo $user password on $host will expire in $chtime days - please log i
n and reset it | mail -s \"Password on $host\" $email_user_name\@mailserver";
}

I advice you for any future Perl questions to use this dedecated forum for Perl:


I hope you find my modification working as i don't have an aix box to try them!

Good luck.

Regards,
Khalid
 
Oh by the way, I forgot to mention that all you need to do with the above script is to change the vairable i created "my $email_user_name = 'khalid'" to include your email account and later in the script you need to change every occurance of @mailserver to your mail server.

For example this could be done as

my $email_user_name = 'THEaix'

and later you change the mail server to to @gmail.com

Which will construct THEaix@gmail.com as your email address.

Regards,
Khalid
 
Thanks for the help and the link. While I was waiting for someone I put this one together. What do you think?

You list the users email and it will mail them as well as sysadmin (root).

#!/usr/bin/perl
use strict;

# password expiration and warning interval

my $exptime = 56;
my $warntime = 42;

# list of users and their email addresses

my %emails =
(
'root' => 'walt.disney@dis.com',
'm.mouse' => 'mickey.mouse@dis.com',
'mn.mouse' => 'mini.mouse@dis.com',

);

# collect users from /etc/password and skip over those whose uid
# is less than 9

my @users;
open FH, "/etc/passwd";
while (<FH>)
{
my ($uname, undef, $uid) = split /:/;

$uid > 9 and
push @users, $uname;
}
close FH;

# do some calculations

my $secs_in_day = 60 * 60 * 24;
my $now = int ((time) / $secs_in_day);

my $host = `uname -n`;
chomp $host;

# process each user we found in /etc/password

my $admin = '';
foreach my $user (@users)
{

# check to see if user in /etc/passwd exists in /etc/security

$admin .= "$user not on system\n" and next
if system "lsuser $user >/dev/null 2>&1";

# obtain user's password last update timestamp (in seconds)

my $chtime = `lssec -f /etc/security/passwd -s $user -a lastupdate`;

# extract timestamp, or set to 0 if its empty.

$chtime =~ s/^.*?=(\d+)/$1/
or $chtime = 0;

# if user never changed password, then note the fact for administrator

$admin .= "$user has never been updated\n" and next
if !$chtime;

# convert date of last update from # of seconds into # of days

$chtime = $now - int ($chtime / $secs_in_day);

# if the password has not reached warning limit, then skip

next
if $chtime < $warntime;

# compute how many days until user's password expires

my $message = "$user password on $host will expire in ".
($exptime - $chtime)." days\n";

# if password already expired, then change message accordingly

$message = "$user password on $host has expired.\n"
if $chtime > $exptime;

# if the user have email address, then send the warning email message

if (exists $emails{$user})
{
$message .= "$user notified by email ".$emails{$user}."\n";
system "echo \"$message\" | mail -s \"Password on $host\" ".$emails{$user};
}

# append message to list of messages to send to administrator

$admin .= $message . "\n";
}

# at end send all of accumlated messages off to administrator

system "echo \"$admin\" | mail -s \"Passwords on $host\" ".$emails{'root'}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top