1: #!/usr/bin/perl
   2: # script to show users' csh history with dates
   3: # ammended to add support for displaying all user's history files
   4: # on a given date (switch -d)
   5: # useful for running daily from a cron job and having 
   6: # the output mailed to an admin
   7: 
   8: =comment
   9: Addendum - the shell script I run from cron around
  10: 11.50pm daily is as follows:
  11: #!/bin/sh
  12: TODAY=`date | awk '{print $2,$3}'
  13: 
  14: /home/munk/bin/perl/showhistory.pl -d "$TODAY"
  15: =cut
  16: 
  17: use strict;
  18: use Getopt::Std;
  19: 
  20: my %opt=();
  21: my($user, $date, $histdate, $histfile, $err);
  22: my $div="#"x68;
  23: 
  24: my $progname = $0;
  25: $progname =~ s,.*/,,;  # use basename only
  26: $progname =~ s/\.\w*$//; # strip extension, if any
  27: 
  28: # find out what options we're passed:
  29: &getopts('u:d:h', \%opt);
  30: 
  31: # display usage if bad opts:
  32: if($opt{h}){ &usage(); }
  33: 
  34: # parse options:
  35: if($opt{u}){
  36: 	&showUserHistory($opt{u});	
  37: } elsif ($opt{d}) {
  38: 	&showDateHistory($opt{d});
  39: } else {
  40: 	&usage("No options specified\n");
  41: }
  42: 
  43: sub showUserHistory {
  44: 	($user, $date) = @_;
  45: 	chomp($date);
  46: 
  47: 	# make sure the date format is right (for matching against system 'date' cmd):
  48: 	$date=~s/(\w+) (\d)$/$1  $2/;
  49: 	
  50: 	my $output="";
  51: 	my $line;
  52: 	my $header="${div}\nHistory for user $user:\n$div\n";
  53: 	
  54: 	$histfile="/home/$user/.history";
  55: 	open(FD, "$histfile") or ($opt{u} && print "${div}\nCouldn't open history file $histfile\n$div\n");
  56: 
  57: 	while(<FD>){
  58: 		s/^#//;
  59: 		
  60: 		$histdate=`date -r $_`;
  61: 		chomp($histdate);
  62: 		# if this history command occurs on the date passed to us, 
  63: 		# or if we're just listing all commands for a user,
  64: 		# append the command history to the $output vbl:
  65: 		my $cmd=<FD>;
  66: 		if( ($histdate=~/$date/) || $opt{u} ){
  67: 			$output.=sprintf("%s %40s", $histdate, $cmd);
  68: 		}
  69: 	}
  70: 	
  71: 	# if there's anything in output, show it, otherwise print a msg:
  72: 	if($output){
  73: 		print $header.$output;
  74: 	} else {
  75: 		# only if single user, otherwise omit:
  76: 		$opt{u} && print "\n${div}\nNo history for user $user\n$div\n";
  77: 	}
  78: }
  79: 
  80: sub showDateHistory{
  81: 	$date=shift;
  82: 
  83: 	opendir(DH, "/home/");
  84: 
  85: 	while(($user=readdir DH)){
  86: 		if($user ne "." && $user ne ".."){
  87: 			&showUserHistory($user, $date);
  88: 		}
  89: 	}
  90: }
  91: 
  92: sub usage{
  93: 	$err && (print $err,"\n");
  94: 	die<<"~USAGE~";
  95: Usage: $progname [-u <user>] [-d <date>] [-h]
  96:        -h                     Display this help.
  97: 
  98:        -d <date>              Display shell history listing for all users on
  99:                               date <date>.
 100:                               Example: $progname -d "Oct 13"
 101:                               Displays shell history listings for all users
 102:                               who logged in on Oct 13 this year.
 103: 
 104:        -u <user>              Display complete shell history listing for 
 105:                               the user <user>.
 106:                               Example: $progname -u munk
 107:                               Display a complete shell history listing for
 108:                               the user 'munk'.
 109: ~USAGE~
 110: }