The script takes various arguments: -d Display shell history listing for all users on date . Example: $progname -d "Oct 13" Displays shell history listings for all users who logged in on Oct 13 this year. -u Display complete shell history listing for the user .
Example: $progname -u munk Display a complete shell history listing for the user 'munk'. The script uses the Getopt::Std perl module. Code follows:
#!/usr/bin/perl
# script to show a user's csh history with dates
# 14/10/2002 ammended to add support for
# displaying all user's history files
# on a given date (switch -d)
# useful for running daily from a cron job
# for the paranoid/careful admin
use strict;
use Getopt::Std;
#require 'getopts.pl';
my %opt=();
my($user, $date, $histdate, $histfile, $err);
my $div="#"x68;
my $progname = $0;
$progname =~ s,.*/,,; # use basename only
$progname =~ s/.w*$//; # strip extension, if any
# find out what options we're passed:
&getopts('u:d:h', %opt);
# display usage if bad opts:
if($opt{h}){ &usage(); }
# parse options:
if($opt{u}){
&showUserHistory($opt{u});
} elsif ($opt{d}) {
&showDateHistory($opt{d});
} else {
&usage("No options specified
");
}
sub showUserHistory {
($user, $date) = @_;
chomp($date);
# make sure the date format is right
#(for matching against system 'date' cmd):
$date=~s/(w+) (d)$/$1 $2/;
my $output="";
my $line;
my $header="${div}
History for user $user:
$div
";
$histfile="/home/$user/.history";
open(FD, "$histfile") or ($opt{u} && print "${div}
Couldn't open history file $histfile
$div
");
while(){
s/^#//;
$histdate=`date -r $_`;
chomp($histdate);
# if this history command occurs on the date passed to us,
# or if we're just listing all commands for a user,
# append the command history to the $output vbl:
my $cmd=;
if( ($histdate=~/$date/) || $opt{u} ){
$output.=sprintf("%s %40s", $histdate, $cmd);
}
}
# if there's anything in output, show it, otherwise print a msg:
if($output){
print $header.$output;
} else {
# only if single user, otherwise omit:
$opt{u} && print "
${div}
No history for user $user
$div
";
}
}
sub showDateHistory{
$date=shift;
opendir(DH, "/home/");
while(($user=readdir DH)){
if($user ne "." && $user ne ".."){
&showUserHistory($user, $date);
}
}
}
sub usage{
$err && (print $err,"
");
die] [-d ] [-h]
-h Display this help.
-d Display shell history listing for all users on
date .
Example: $progname -d "Oct 13"
Displays shell history listings for all users
who logged in on Oct 13 this year.
-u Display complete shell history listing for
the user .
Example: $progname -u munk
Display a complete shell history listing for
the user 'munk'.
~USAGE~
}
This article was originally written by munk |