#!/usr/bin/perl # script to show users' csh history with dates # 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 and having # the output mailed to an admin =comment Addendum - the shell script I run from cron around 11.50pm daily is as follows: #!/bin/sh TODAY=`date | awk '{print $2,$3}' /home/munk/bin/perl/showhistory.pl -d "$TODAY" =cut use strict; use Getopt::Std; 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\n"); } 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}\nHistory for user $user:\n$div\n"; $histfile="/home/$user/.history"; open(FD, "$histfile") or ($opt{u} && print "${div}\nCouldn't open history file $histfile\n$div\n"); 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 "\n${div}\nNo history for user $user\n$div\n"; } } sub showDateHistory{ $date=shift; opendir(DH, "/home/"); while(($user=readdir DH)){ if($user ne "." && $user ne ".."){ &showUserHistory($user, $date); } } } sub usage{ $err && (print $err,"\n"); die<<"~USAGE~"; Usage: $progname [-u ] [-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~ }