#!/usr/bin/perl # # v1.0 # lastlogin.pl - Reports what users have not logged on in a specifed amount # of time. # Aaron W. Morris # Options: #h (hours), #d (days), #w (weeks), #m (months), #y (years) # Example: lastlogin.pl 3h 4d 8m 1y use strict; if(! @ARGV) { print "Usage: \"lastlogin.pl 5d 2m 1y\"\n"; exit 1; } my @periods = @ARGV; my $ltime = time(); my $i = 0; my $x = 0; my $hour = 3600; my $day = $hour * 24; my $week = $day * 7; my $month = $day * 30; my $year = $day * 365; my $run = $#periods + 1; #change args into seconds my @times; my $seconds; while($run != 0) { if($periods[$i] =~ /^(\d{1,})(h|d|w|m|y)$/i) { $seconds = &getseconds($1,$2); $times[$x] = $seconds; $x++; } $i++; $run--; } my @stimes = &sorttimes(@times); #read `lsuser ALL` my %users; my @lsuser = `/usr/sbin/lsuser ALL`; foreach(@lsuser) { my $line = $_; my @items; my %userinfo; chomp $line; @items = split(/\s/,$line); %userinfo = &makehash(@items); $users{getpwuid($userinfo{'id'})} = $userinfo{'time_last_login'}; } #process entries and assign to appropriate anonymous hashes my $key; my $value; my $lastlog; while( ($key,$value) = each(%users) ) { next if(! $value); for(0 .. $#stimes) { if($value <= ($ltime - $stimes[$_]) ) { $lastlog->{$stimes[$_]}->{$key} = $value; last; } } } #output info my $anonref; my $timeper; for(0 .. $#stimes) { $timeper = &gettimeper($stimes[$_]); print "\n\n>$timeper\n"; $anonref = $lastlog->{$stimes[$_]}; while( ($key,$value) = each(%$anonref) ) { print "$key "; } print "\n"; } print "\n"; #end sub makehash { my @items = @_; my %infohash; my $element; my @info; foreach $element (@items) { @info = split(/\=/,$element); $infohash{$info[0]} = $info[1]; } return %infohash; } #end sub makehash sub getseconds { my $numperiod = $_[0]; my $per = $_[1]; my $seconds; $per =~ tr/[A-Z]/[a-z]/; if($per eq "h") { $seconds = $hour; } elsif($per eq "d") { $seconds = $day; } elsif($per eq "w") { $seconds = $week; } elsif($per eq "m") { $seconds = $month; } elsif($per eq "y") { $seconds = $year; } return ($seconds * $numperiod); } #end sub getseconds sub sorttimes { my @times = @_; my $change = 1; until($change == 0){ $change = 0; for(0 .. ($#times - 1)) { my $pos = $_; my $temp1; my $temp2; if($times[$pos] < $times[($pos + 1)]) { $temp1 = $times[$pos]; $temp2 = $times[($pos + 1)]; $times[$pos] = $temp2; $times[($pos + 1)] = $temp1; $change = 1; } elsif($times[$pos] == $times[($pos + 1)]) { splice(@times,$pos,1); @times = &sorttimes(@times); $change = 1; last; } } } return(@times); } #end sub sorttimes sub gettimeper { my $seconds = $_[0]; my $message; my $howmany; if( ($seconds % $year) == 0 ) { $howmany = $seconds / $year; $message = $howmany . " Year(s)"; } elsif( ($seconds % $month) == 0 ) { $howmany = $seconds / $month; $message = $howmany . " Month(s)"; } elsif( ($seconds % $week) == 0 ) { $howmany = $seconds / $week; $message = $howmany . " Week(s)"; } elsif( ($seconds % $day) == 0 ) { $howmany = $seconds / $day; $message = $howmany . " Day(s)"; } elsif( ($seconds % $hour) == 0 ) { $howmany = $seconds / $hour; $message = $howmany . " Hour(s)"; } return($message); } #end sub gettimeper