Originally posted by: SystemAdmin
You may find the following to be of use.
Regards,
George
#!/usr/bin/perl
-
@(#)hist - Processes the command history list of any user - GLS 02/10/2008
#=============================================================================#
-
hist Display the history of a user; similar output to history command #
-
#
-
Usage: hist user default is history of current user #
-
#
-
Created: G L Spencer 02/10/2008 #
#=============================================================================#
-
Initialization
#
$delim_1="#" . chr(0336) . "#"; # Initial delimiter in .sh_history
$delim_2="#" . chr(0337) . "#"; # Final delimiter in .sh_history
-
Determine the user requested
#
$user=@ARGV[0];
chop($who_am_i=`who am i`); # Chop to remove the newline
split(' ', $who_am_i); # Split on spaces to @_ array
if ($user eq "") {$user=@_[0];}
-
Check whether HELP is required
#
if ( $user eq "-h" || $user eq "-?" ) {
print "Usage: hist
user\n";
print " Displays the user shell history with timestamp. The output is similar \n";
print " to that displayed by the 'history -t' command. The default user is the\n";
print " user running the program. To view the history of another user, you must\n";
print " run the program as the root user.\n";
exit 1;
}
-
Ensure that program is run as root to read another user's .sh_history
#
if ($user ne @_[0]) {
chop($asUser=`whoami`); # Chop to remove the newline
if ($asUser ne "root") {
print "WARNING: hist program must be run as root to read the .sh_history\n";
print " of another user.\n";
exit 1;
}
}
-
Determine the location of the home directory
#
@pass=getpwnam($user); # Get /etc/passwd details in array
$userDir=@pass[7];
if (@pass[0] eq "") {
print "User, $user, no longer exists, ";
if ( -e "/home/$user/.sh_history") {
print "but home directory is present.\n";
} else {
print "and no home directory.\n";
exit 2;
}
}
-
Read and output the .sh_history file
#
$histFile="$userDir/.sh_history";
if (! -e $histFile) {print "$userDir/.sh_history not found"; exit 3;};
open(HIST, "<$histFile");
while (<HIST>) {
chop; # Chop the newline
$cnt++;
s/\0//g; # Remove nulls
@fld=/(
^\0+)$delim_1(.*)$delim_2/; # Split line at delimiters
if ($#fld == 1) { # Two fields
($sec, $min, $hr, $dd, $mon, $yr, $wday, $yday, $isd)=localtime($fld[1]);
printf("%-8d", $cnt);
printf("%02d/%02d/%04d %02d:%02d:%02d :: ",$dd,$mon+1,1900+$yr,$hr,$min,$sec);
printf("$fld[0]\n");
} else {
if ($_ eq "") {next;} # Skip null string lines
printf("%-27d :: ", $cnt);
print "$_\n";
}
}